def bgrun(self): self.parent_pipe, self.child_pipe = aioprocessing.AioPipe() evloop_process = multiprocessing.Process(target=self.run, args=()) evloop_process.start() time.sleep(4) self.parent_pipe.send("exit") evloop_process.join()
def __init__(self, ip_address, max_agents, director_hostname, connector, logger_str): self.ip_address = ip_address self.director_hostname = director_hostname self.max_agents = max_agents self.agents_in_use = 0 self.takes_new_scenarios = True self.scenario_runs = {} self.logger_str = logger_str # setup multiprocessing environment self.send_queue = aioprocessing.AioQueue() self.manager = multiproc.Manager() self.pipe_dict = self.manager.dict() self.receive_pipe, self.pipe_dict[ "supervisor"] = aioprocessing.AioPipe(False) # setup logger semaphores for all possible scenario runs self.logger_semaphores = [{ "semaphore": self.manager.Semaphore(1), "used_by": "" } for i in range(max_agents)] # setup observations_done lists self.observations_done = [{ "list": self.manager.list(), "used_by": "" } for i in range(max_agents)] # get correct connector to director module = importlib.import_module( "connectors." + re.sub("([A-Z])", "_\g<1>", connector).lower()[1:]) connector_class = getattr(module, connector) self.connector = connector_class(director_hostname, max_agents, self.send_queue, self.pipe_dict)
def start(self): self.pending_cmds = {} self.channel, client_channel = aioprocessing.AioPipe() self.stdout_q = sout = aioprocessing.AioQueue() self.stderr_q = serr = aioprocessing.AioQueue() self.listener = asyncio.ensure_future(self.listen_task()) loop = asyncio.get_event_loop() kernel = self.slave(client_channel, sout, serr) self.process = p = aioprocessing.AioProcess(target=kernel.start) p.start()
def test_pipe(self): conn1, conn2 = aioprocessing.AioPipe() val = 25 p = Process(target=conn_send, args=(conn1, val)) p.start() async def conn_recv(): out = await conn2.coro_recv() self.assertEqual(out, val) self.loop.run_until_complete(conn_recv())
def fork_and_start(main_function: Callable): """ Forks a child process to run as the docker controller, and keep running the `main_function()` as the main process. When the `main_function()` exits, the child process is notified via `terminate_evt`, which will cause the child process to exit immediately as well. Afterwards, call join() and close() on the child process to ensure all acquired resources are freed up. """ p1, p2 = aioprocessing.AioPipe() terminate_evt: aioprocessing.AioEvent = aioprocessing.AioEvent() docker_process: Process = Process(target=_docker_process_main, args=(p1, p2, terminate_evt)) try: # fork the docker process as child. docker_process.start() # Set the pipe for docker_ipc() functions. set_hummingbot_pipe(p1) # run the main function as parent. main_function() # stop the gateway container. try: asyncio.get_event_loop().run_until_complete( docker_ipc("stop", container=get_gateway_container_name(), timeout=1)) except Exception: pass finally: # close pipes. p1.close() p2.close() # set the terminate event. terminate_evt.set() # wait for Docker controller process to clean up. docker_process.join() docker_process.close()
def main(): port_identifier = None for port in list_ports.comports(): # Check for VendorID and ProductID in hardware id of antenna if "0403:6001" in port.hwid: # port identifier is first part of str(port) (until whitespace) port_identifier = str(port).split()[0] print(f"Portnumber: {port_identifier}") if port_identifier is None: raise IOError("Antenna is not connected!") left_pipe_end, right_pipe_end = aioprocessing.AioPipe() # separate process that contains communication and data processing # pass one end of the pipe so data can be sent to this process p = aioprocessing.AioProcess(target=dataproducer.communication, args=(port_identifier, left_pipe_end)) p.start() # pass other end of the pipe to the websocket server, to receive telemetry # from the other process websocket.start_server(right_pipe_end)
def run(self): """ Represents the run method of a supervisor who receives messages from the director and sends scenario dependent other messages to the director. It uses a connector class to handle the actual connection to the director and a ScenarioRun objects to represent each scenario run by one subprocess. :rtype: None """ self.connector.start() while self.takes_new_scenarios: received_msg = self.receive_pipe.recv() if received_msg['type'] == "scenario_end": finished_scenario_run_id = received_msg['scenario_run_id'] finished_scenario_run = self.scenario_runs[ finished_scenario_run_id] finished_scenario_run.terminate() finished_scenario_run.join() del self.scenario_runs[finished_scenario_run_id] index_logger = [ i for i in range(len(self.logger_semaphores)) if self.logger_semaphores[i]["used_by"] == finished_scenario_run_id ][0] self.logger_semaphores[index_logger]["used_by"] = "" index_done = [ i for i in range(len(self.observations_done)) if self.observations_done[i]["used_by"] == finished_scenario_run_id ][0] self.observations_done[index_done]["used_by"] = "" self.send_queue.put(received_msg) elif received_msg['type'] == 'scenario_registration': print('Received Scenario Registration') # TODO: check if enough agents are left and scenario can be really started new_scenario_run_id = received_msg["scenario_run_id"] self.agents_in_use += len(received_msg["agents_at_supervisor"]) recv_end, send_end = aioprocessing.AioPipe(False) self.pipe_dict[new_scenario_run_id] = send_end # creating logger for new scenario run with already registered semaphore index_logger, logger_semaphore_dict = next( (index, semaphore) for (index, semaphore) in enumerate(self.logger_semaphores) if semaphore["used_by"] == "") logger_semaphore = logger_semaphore_dict["semaphore"] self.logger_semaphores[index_logger][ "used_by"] = new_scenario_run_id module = importlib.import_module( "loggers." + re.sub("([A-Z])", "_\g<1>", self.logger_str).lower()[1:]) logger_class = getattr(module, self.logger_str) logger = logger_class(new_scenario_run_id, logger_semaphore) # taking one observations_done list index_done, done_dict = next( (index, obs_done_dict) for (index, obs_done_dict) in enumerate(self.observations_done) if obs_done_dict["used_by"] == "") observations_done = done_dict["list"] self.observations_done[index_done][ "used_by"] = new_scenario_run_id # create and start scenario_run new_scenario_run = ScenarioRun( new_scenario_run_id, received_msg["agents_at_supervisor"], Scenario(**received_msg["scenario"]), self.ip_address, self.send_queue, recv_end, logger, observations_done, self.pipe_dict["supervisor"]) self.scenario_runs[new_scenario_run_id] = new_scenario_run new_scenario_run.start()
def bgrun(self): print('Starting evloop in different process') self.parent_pipe, self.child_pipe = aioprocessing.AioPipe() self.evloop_process = multiprocessing.Process(target=self.run, args=()) self.evloop_process.start()