""" Example of an IPC server providing date information to a client """ # pylint: disable=no-name-in-module from time import asctime from sysv_ipc import MessageQueue, IPC_CREX if __name__ == "__main__": # Create IPC with key 0x80 if it doesn't exist KEY = 128 mq = MessageQueue(KEY, IPC_CREX) while True: message, t = mq.receive() # 1 : date request if t == 1: message = asctime().encode() mq.send(message, type=3) print(f"date {message} sent") # 2 : stop request elif t == 2: print("terminating") mq.remove() break
def datetime_request(queue, pid): """ Handles a time request asynchronously :param queue: the queue to send the date back into :param pid: the request client pid """ time = asctime().encode() queue.send(time, type=(int(pid) + 3)) print(f"date {time} sent to client {pid}") if __name__ == "__main__": # Create IPC with key 0x80 if it doesn't exist KEY = 0x80 message_q = MessageQueue(KEY, IPC_CREX) with concurrent.futures.ThreadPoolExecutor(max_workers=8) as pool: while True: message, message_type = message_q.receive() # 1 : date request if message_type == 1: # Do it async pool.submit(datetime_request, message_q, message) # 2 : stop request elif message_type == 2: print("terminating") message_q.remove() break
def market(weatherAttributes, prices, exitFlag): print("[%d] Market (main) : Init" % getpid()) # create handler with a closure, reroute signals, and launch external process externalEvents = [0, 0] def market_handler(sig, frame): if sig == SIGUSR1: externalEvents[0] = 1 elif sig == SIGUSR2: externalEvents[1] = 1 signal(SIGUSR1, market_handler) signal(SIGUSR2, market_handler) externalProcess = Process(target=external, args=(getpid(), exitFlag)) externalProcess.start() # create lock that will be passed to many threads to treat each request sharedLock = Lock() # create message queues to Homes queueToHome = MessageQueue(marketKey, IPC_CREAT) # if queue was not properly deleted, it's still there and we have to do it ourselves if queueToHome.current_messages > 0: queueToHome.remove() queueToHome = MessageQueue(marketKey, IPC_CREAT) print("[%d] Market (main) : messageQueue created" % getpid()) buying = [] selling = [] idx = 0 price = -1 gamma = 0.90 # long-term attenuation coefficient alpha = [ -0.052, 0.075, 0.23, -0.07 ] # modulating coefficient for internal factors: Temperature, Rain, bought, sold internalEvents = [0, 0, 0, 0] beta = [-7, 30 ] # modulating coefficient for external factors: SIGUSR1, SIGUSR2 # print("[%d] Market (main) : all external processes launched" % getpid()) while not exitFlag.value: sleep(period) # upon receiving a buying message, we launch a thread try: while queueToHome.current_messages > 0: message, _ = queueToHome.receive(block=False, type=1) (Thread(target=marketThread, args=( message, buying, sharedLock, ))).start() except (NotAttachedError, BusyError): pass # upon receiving a selling message, we launch a thread try: while queueToHome.current_messages > 0: message, _ = queueToHome.receive(block=False, type=2) (Thread(target=marketThread, args=( message, selling, sharedLock, ))).start() except (NotAttachedError, BusyError): pass print( "[%d] external = %s \tweather = [ %.2f, %.2f ]\tbuying = %s\tselling = %s" % (getpid(), externalEvents, weatherAttributes[0], weatherAttributes[1], buying, selling)) internalEvents = [ weatherAttributes[0], weatherAttributes[1], sum(buying), sum(selling) ] if price == -1: print("[%d] Initiating energy price" % getpid()) price = sum([ alpha[k] * internalEvents[k] for k in range(len(internalEvents)) ]) + sum([ beta[k] * externalEvents[k] for k in range(len(externalEvents)) ]) else: price = gamma * price + sum([ alpha[k] * internalEvents[k] for k in range(len(internalEvents)) ]) + sum([ beta[k] * externalEvents[k] for k in range(len(externalEvents)) ]) if price <= 0: price = 0.05 prices[idx] = price idx = (idx + 1) % 500 #print("In market : "+str(prices[0:20])) print("[%d] Energy price = %.2f €/kWh" % (getpid(), price)) externalEvents = [0, 0] buying = [] selling = [] queueToHome.remove() print("[%d] Market (main) : messageQueue removed" % getpid()) print("[%d] Market (main) : Exit" % getpid())