async def test_scp(event_loop): # ensure that asyncio.subprocess will work; try: asyncio.get_child_watcher().attach_loop(event_loop) except RuntimeError: pytest.skip('test_scp will always fail outside of MainThread') async with base.CleanModel() as model: await model.add_machine() await asyncio.wait_for( model.block_until(lambda: model.machines), timeout=240) machine = model.machines['0'] await asyncio.wait_for( model.block_until(lambda: (machine.status == 'running' and machine.agent_status == 'started')), timeout=480) with NamedTemporaryFile() as f: f.write(b'testcontents') f.flush() await machine.scp_to(f.name, 'testfile', scp_opts='-p') with NamedTemporaryFile() as f: await machine.scp_from('testfile', f.name, scp_opts='-p') assert f.read() == b'testcontents'
def wrapper(*args, **kwargs): with loop_context() as loop: injector = Injector(loop=loop, fake_future=lambda: fake_future) asyncio.get_child_watcher().attach_loop(loop) asyncio.set_event_loop(loop) loop.run_until_complete(injector.call(f, *args, **kwargs))
def __init__(self, protocol_cls, loop=None, max_workers=2, sync_kind=TextDocumentSyncKind.INCREMENTAL): if not issubclass(protocol_cls, asyncio.Protocol): raise TypeError( 'Protocol class should be subclass of asyncio.Protocol') self._max_workers = max_workers self._server = None self._stop_event = None self._thread_pool = None self._thread_pool_executor = None self.sync_kind = sync_kind if IS_WIN: asyncio.set_event_loop(asyncio.ProactorEventLoop()) else: asyncio.set_event_loop(asyncio.SelectorEventLoop()) self.loop = loop or asyncio.get_event_loop() try: asyncio.get_child_watcher().attach_loop(self.loop) except NotImplementedError: pass self.lsp = protocol_cls(self)
def __init__(self, builder, *args, **kwargs): """ Build the ThreadManager. :param builder: The API function to use """ self._loop = None self.managed_obj = None self.bridged_obj = None self._sync_managed_obj = None is_running = threading.Event() self._is_running = is_running # TODO: remove this if we switch to python 3.8 # https://docs.python.org/3/library/asyncio-subprocess.html#subprocess-and-threads # noqa # On windows, the event loop and system interface is different and # this won't work. try: asyncio.get_child_watcher() except NotImplementedError: pass blocking = not kwargs.pop('threadmanager_nonblocking', False) target = object.__getattribute__(self, '_build_and_start_loop') thread = threading.Thread(target=target, name='ManagedThread', args=(builder, *args), kwargs=kwargs, daemon=True) self._thread = thread thread.start() if blocking: object.__getattribute__(self, 'managed_thread_ready_blocking')()
async def test_scp(event_loop): # ensure that asyncio.subprocess will work; try: asyncio.get_child_watcher().attach_loop(event_loop) except RuntimeError: pytest.skip('test_scp will always fail outside of MainThread') async with base.CleanModel() as model: app = await model.deploy('ubuntu') await asyncio.wait_for(model.block_until(lambda: app.units), timeout=60) unit = app.units[0] await asyncio.wait_for(model.block_until(lambda: unit.machine), timeout=60) machine = unit.machine await asyncio.wait_for( model.block_until(lambda: (machine.status == 'running' and machine. agent_status == 'started')), timeout=480) with NamedTemporaryFile() as f: f.write(b'testcontents') f.flush() await unit.scp_to(f.name, 'testfile') with NamedTemporaryFile() as f: await unit.scp_from('testfile', f.name) assert f.read() == b'testcontents'
def setup_asyncio(self): ''' this ensures that the child process watcher is running on the main thread, which is necessary for subprocess to be waited for, as python can only catch SIG_CHLD on the main thread. additionally, an event loop must be executed on the main thread during waiting. ''' asyncio.get_child_watcher()
def ctags(self): asyncio.get_child_watcher() self.proteome.proteome_start() self.pros.foreach(lambda a: self.proteome.pro_add([a.ident])) self._await() self.proteome.pro_save() self._await() later(lambda: self.pros.foreach(lambda a: a.tag_file.should.exist))
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started. Also seems to require a child watcher attached to the loop when run from pytest. """ self.hass = get_test_home_assistant() asyncio.get_child_watcher().attach_loop(self.hass.loop)
async def __aenter__(self): asyncio.get_child_watcher().attach_loop(self.loop) self.proc = await asyncio.create_subprocess_exec(*self.args, loop=self.loop) logger.debug('Started, waiting for debug port...') await asyncio.sleep(1) # Not sure why this is necessary... await wait_for_port(self.host, self.port, loop=self.loop) logger.debug('Debug port available.')
def setUp(self): # pylint: disable=invalid-name """Set up things to be run when tests are started. Also seems to require a child watcher attached to the loop when run from pytest. """ self.hass = get_test_home_assistant() asyncio.get_child_watcher().attach_loop(self.hass.loop)
def __init__(self): print("Initialisation of the scheduler manager") self.scheduler = BackgroundScheduler() # create the async loop in the main thread self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) # bind event loop to current thread asyncio.get_child_watcher().attach_loop(self.loop)
def thread_check_notification(self): asyncio.set_event_loop(self.scrapper.loop) asyncio.get_child_watcher().attach_loop(self.scrapper.loop) thread = threading.Thread( target=lambda: self.scrapper.loop.run_until_complete( self.loop_check_notification()), daemon=True) thread.start()
def main(): servers = ['www.google.com']*5 event_loop = asyncio.get_event_loop() asyncio.get_child_watcher() pool = TaskPool(event_loop, ASYNCIO_TASK_POOL) thread = Thread(target=event_loop.run_until_complete, args=(pool.join(),)) thread.start() for server in servers: pool.submit(ConsoleHealthCheck(server).run_health_check())
async def _run_subprocess( command: List[str], stdout_handler: Optional[RunSubprocessHandler] = None, stderr_handler: Optional[Callable[[str], Coroutine]] = None, env: Optional[dict] = None, cwd: Optional[str] = None, wait: bool = True, ) -> asyncio.subprocess.Process: """ Run a command as a subprocess and handle stdin and stderr output line-by-line. :param command: The command to run as a subprocess. :param stdout_handler: A function to handle stdout lines. :param stderr_handler: A function to handle stderr lines. :param env: Environment variables to set for the subprocess. :param cwd: Current working directory for the subprocess. :param wait: Flag indicating to wait for the subprocess to finish before returning. """ logger.info(f"Running command in subprocess: {' '.join(command)}") # Ensure the asyncio child watcher has a reference to the running loop, prevents `process.wait` from hanging. asyncio.get_child_watcher().attach_loop(asyncio.get_running_loop()) stdout = asyncio.subprocess.PIPE if stdout_handler else asyncio.subprocess.DEVNULL if stderr_handler: async def _stderr_handler(line): await stderr_handler(line) logger.info(f"STDERR: {line.rstrip()}") else: async def _stderr_handler(line): logger.info(f"STDERR: {line.rstrip()}") process = await asyncio.create_subprocess_exec( *(str(arg) for arg in command), stdout=stdout, stderr=asyncio.subprocess.PIPE, env=env, cwd=cwd) _watch_subprocess = asyncio.create_task( watch_subprocess(process, stdout_handler, _stderr_handler)) @hooks.on_failure def _terminate_process(): if process.returncode is None: process.terminate() _watch_subprocess.cancel() if wait: await process.wait() return process
def init(): global client asyncio.get_child_watcher() # uh this function sound weird loop = asyncio.get_event_loop() loop.create_task(start()) thread = threading.Thread(target=run_it_forever, args=(loop, )) thread.start()
def init(): asyncio.get_child_watcher() loop = asyncio.get_event_loop() loop.create_task(start_client()) thread_client = threading.Thread(target=start_client_loop, args=(loop, )) thread_client.start() thread_chain = threading.Thread(target=start_chain_ping, args=(loop, )) thread_chain.start()
def run(background=False): asyncio.get_child_watcher().attach_loop(loop) daemon = ExtProgramRunner() loop.call_soon(daemon.start, loop) # start main event loop if background: executor = None loop.run_in_executor(executor, loop.run_forever) else: loop.run_forever()
def _setup_eventloop(self): """ Sets up a new eventloop as the current one according to the OS. """ if os.name == 'nt': self.eventloop = asyncio.ProactorEventLoop() else: self.eventloop = asyncio.new_event_loop() asyncio.set_event_loop(self.eventloop) if os.name == 'posix' and isinstance(threading.current_thread(), threading._MainThread): asyncio.get_child_watcher().attach_loop(self.eventloop)
def run_async_process(cmd, out, err, **kwargs): if os.name == 'nt': loop = asyncio.ProactorEventLoop() # for subprocess' pipes on Windows asyncio.set_event_loop(loop) else: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) asyncio.get_child_watcher().attach_loop(loop) loop = asyncio.get_event_loop() rc, odata, edata = loop.run_until_complete(read_and_store(cmd, out, err, **kwargs)) return rc, odata, edata
def start_process(self) -> mp.Process: """ Start this executor in a new process """ def _start(): self._init_process() self.start() self._runner.join() asyncio.get_child_watcher() proc = mp.Process(target=_start) proc.start() return proc
def __init__(self, *args, **kwargs): if sys.platform == "win32": self.loop = kwargs.pop('loop', asyncio.ProactorEventLoop()) asyncio.set_event_loop(self.loop) else: self.loop = kwargs.pop('loop', asyncio.get_event_loop()) asyncio.get_child_watcher().attach_loop(self.loop) self.token = kwargs.pop("token") command_prefix = kwargs.pop('command_prefix', commands.when_mentioned_or('+')) max_messages = kwargs.pop('max_messages', 5000) super().__init__(command_prefix=command_prefix, *args, **kwargs)
def test_run_func(self): self.rule.execute_subprocess( self.set_ret, sys.executable, '-c', 'import datetime; print(datetime.datetime.now())', capture_output=True ) # Test this call from __main__ to create thread save process watchers if sys.platform != "win32": asyncio.get_child_watcher() asyncio.get_event_loop().run_until_complete(asyncio.gather(asyncio.sleep(0.5))) self.assertEqual(self.ret.returncode, 0) self.assertTrue(self.ret.stdout.startswith('20'))
def main(): asyncio.get_child_watcher() api_thread = threading.Thread(target=run_api) api_thread.daemon = True api_thread.start() game_thread = threading.Thread(target=run_game_loop) game_thread.daemon = True game_thread.start() rtm_client = slack.RTMClient(token=slack_token) rtm_client.start()
async def _cli_cmnd(self, *args): cmnd_args = [ self.cli_bin, '-datadir={}'.format(self.data_path), '-regtest', '-rpcuser=rpcuser', '-rpcpassword=rpcpassword', '-rpcport=50001' ] + list(args) self.log.info(' '.join(cmnd_args)) loop = asyncio.get_event_loop() asyncio.get_child_watcher().attach_loop(loop) process = await asyncio.create_subprocess_exec( *cmnd_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) out, _ = await process.communicate() self.log.info(out.decode().strip()) return out.decode().strip()
async def start(self): assert self.ensure() self.data_path = tempfile.mkdtemp() loop = asyncio.get_event_loop() asyncio.get_child_watcher().attach_loop(loop) command = (self.daemon_bin, '-datadir={}'.format(self.data_path), '-printtoconsole', '-regtest', '-server', '-txindex', '-rpcuser=rpcuser', '-rpcpassword=rpcpassword', '-rpcport=50001') self.log.info(' '.join(command)) self.transport, self.protocol = await loop.subprocess_exec( lambda: BlockchainProcess(self.log), *command) await self.protocol.ready.wait()
def __init__(self): self.loop = asyncio.get_event_loop() executor_opts = {'max_workers': None} if sys.version_info[:2] >= (3, 6): executor_opts['thread_name_prefix'] = 'SyncWorker' self.executor = ThreadPoolExecutor(**executor_opts) self.loop.set_default_executor(self.executor) self.loop.set_exception_handler(async_loop_exception_handler) if asyncio.get_child_watcher()._loop is not self.loop: asyncio.get_child_watcher().attach_loop(self.loop)
async def server_main_logwrapper(loop, pidx, _args): setproctitle(f"backend.ai: storage-proxy worker-{pidx}") try: asyncio.get_child_watcher() except (AttributeError, NotImplementedError): pass log_endpoint = _args[1] logger = Logger(_args[0]["logging"], is_master=False, log_endpoint=log_endpoint) with logger: async with server_main(loop, pidx, _args): yield
def testPolicyWrapperRecursion(self): initial_policy = asyncio.get_event_loop_policy() if not isinstance(initial_policy, DefaultEventLoopPolicy): asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) try: with self.assertRaises(NotImplementedError): asyncio.get_event_loop() with self.assertRaises(NotImplementedError): asyncio.get_child_watcher() finally: asyncio.set_event_loop_policy(initial_policy)
def _log_agents(self, tokens): asyncio.get_child_watcher() i = 0 for el in tokens: self.clients.append( MyDiscordAgent(users_under_control, log_en=True, human=False)) self.clients_loop.append(asyncio.get_event_loop()) self.clients_loop[i].create_task( self._start_agent_actions(self.clients[i], el)) self.clients_thread.append( threading.Thread(target=self._run_it_forever, args=(self.clients_loop[i], ))) self.clients_thread[i].start() i += 1 sleep(0.2)
def get_event_loop(): # https://stackoverflow.com/questions/44684898/python-asyncio-running-subprocess-exec-on-a-worker-thread asyncio.get_child_watcher() loop = asyncio.get_event_loop() if loop.is_closed(): print('opening new loop') loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) def debug_exception_handler(loop, context): print(context) loop.set_debug(True) loop.set_exception_handler(debug_exception_handler) return loop
def run(args): import os if os.environ.get("PYTHONASYNCIODEBUG"): import logging logging.basicConfig(level=logging.DEBUG) import warnings warnings.simplefilter('default') import asyncio loop = asyncio.get_event_loop() asyncio.get_child_watcher() from url_sources.capture_phantom import CaptureDispatcher loop.run_until_complete(CaptureDispatcher(args).run()) loop.close()
def __execute(cmd, stdout_cb, stderr_cb): try: loop = asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) asyncio.get_child_watcher().attach_loop(loop) rc = loop.run_until_complete( ShellExecutor._stream_subprocess( cmd, stdout_cb, stderr_cb, )) print("Complete executing command. Returning from __execute") return rc
async def start(self): assert self.ensure() self.data_path = tempfile.mkdtemp() loop = asyncio.get_event_loop() asyncio.get_child_watcher().attach_loop(loop) command = [ self.daemon_bin, f'-datadir={self.data_path}', '-printtoconsole', '-regtest', '-server', '-txindex', f'-rpcuser={self.rpcuser}', f'-rpcpassword={self.rpcpassword}', f'-rpcport={self.rpcport}', f'-port={self.peerport}' ] self.log.info(' '.join(command)) self.transport, self.protocol = await loop.subprocess_exec( BlockchainProcess, *command) await self.protocol.ready.wait()
def chThread(self): bot = CatchHelperBot(self._pause_time, self._include_play, self._include_pause, self._include_stop, self._device_ids, self._mad, description="Bot to restart the PoGo app") asyncio.get_child_watcher() loop = asyncio.get_event_loop() sch_worker = Thread(name="ShinyCatchHelper", target=self.run_CatchHelper_forever, args=(loop, bot)) sch_worker.daemon = True sch_worker.start()
def testPolicyWrapperRecursion(self): if asyncio is None: self.skipTest('asyncio is not available') initial_policy = asyncio.get_event_loop_policy() if not isinstance(initial_policy, DefaultEventLoopPolicy): asyncio.set_event_loop_policy(DefaultEventLoopPolicy()) try: with self.assertRaises(NotImplementedError): asyncio.get_event_loop() with self.assertRaises(NotImplementedError): asyncio.get_child_watcher() finally: asyncio.set_event_loop_policy(initial_policy)
def test_run_func_no_cap(self): self.rule.execute_subprocess( self.set_ret, sys.executable, '-c', 'import datetime; print(datetime.datetime.now())', capture_output=False) # Test this call from __main__ to create thread save process watchers if sys.platform != "win32": asyncio.get_child_watcher() loop.run_until_complete(asyncio.gather(asyncio.sleep(0.5))) self.assertEqual(self.ret.returncode, 0) self.assertEqual(self.ret.stdout, None) self.assertEqual(self.ret.stderr, None)
def _setup_eventloop(self): """Sets up a new eventloop as the current one according to the OS.""" if os.name == "nt": self.eventloop = asyncio.ProactorEventLoop() else: try: self.eventloop = asyncio.get_event_loop() except RuntimeError: if threading.current_thread() != threading.main_thread(): # Ran not in main thread, make a new eventloop self.eventloop = asyncio.new_event_loop() asyncio.set_event_loop(self.eventloop) else: raise if os.name == "posix" and isinstance(threading.current_thread(), threading._MainThread): asyncio.get_child_watcher().attach_loop(self.eventloop)
def wss_server(): ws_address = f"wss://{HOSTNAME}:{WSS_PORT}/WebSocket" loop = asyncio.new_event_loop() asyncio.get_child_watcher()._loop = loop start_server = websockets.serve(handler, "localhost", WSS_PORT, loop=loop) loop.run_until_complete(start_server) loop.run_forever()
def _attach_pid(self, newpid): """ Attach this process to a new PID, creating a condition which will be used by the child watcher to determine when the PID has exited. """ with asyncio.get_child_watcher() as watcher: watcher.add_child_handler(newpid, self._child_watcher_callback) self._exit_event = asyncio.Event()
def __init__(self, *args, **kwargs): self.loop = kwargs.pop('loop', asyncio.get_event_loop()) asyncio.get_child_watcher().attach_loop(self.loop) self.dev_mode = kwargs.pop('dev_mode', False) self.token = os.getenv('bot_token') if not self.dev_mode else os.getenv('bot_beta_token') self.self_bot = kwargs.pop('self_bot', False) if self.self_bot: self.token = os.getenv('notsosuper_token') shard_id = kwargs.get('shard_id', 0) command_prefix = kwargs.pop('command_prefix', commands.when_mentioned_or('.')) init_logging(shard_id, self) super().__init__(command_prefix=command_prefix, *args, **kwargs) self.remove_command('help') init_funcs(self) self.owner = None self.start_time = time.time() self.own_task = None self.last_message = None self.command_messages = {}
def _asyncio_child_watcher(self): """ Portage internals use this as a layer of indirection for asyncio.get_child_watcher(), in order to support versions of python where asyncio is not available. @rtype: asyncio.AbstractChildWatcher @return: the internal event loop's AbstractChildWatcher interface """ return _real_asyncio.get_child_watcher()
def main(): _, dbname, analyzer, runs = sys.argv runs = runs.split(",") loop = asyncio.get_event_loop() # child watcher must be initialized before anything creates threads asyncio.get_child_watcher() with async_pgconn(dbname, loop=loop) as db, \ aiohttp.ClientSession(loop=loop) as http_client, \ rate_limiter(10, loop=loop) as wb_rate, \ rate_limiter(4096, loop=loop) as gt_rate, \ TopicAnalyzer(analyzer, loop=loop) as topic_analyzer: wayback = WaybackMachine(http_client, wb_rate) gtrans = GoogleTranslate(http_client, gt_rate) session = HistoryRetrievalSession( runs, db, wayback, gtrans, topic_analyzer, loop) loop.run_until_complete(loop.create_task(session.get_page_histories()))
def main(): p = ArgumentParser() p.add_argument('codepath', help='path to code root', nargs='?', default='~/code') p.add_argument('mainbranch', nargs='?', default='master', help='name of your main branch') P = p.parse_args() if os.name == 'nt': loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop) else: loop = asyncio.new_event_loop() # necessary since we're returning from subprocess # for other types of applications, asyncio.run() does this implicitly. asyncio.get_child_watcher().attach_loop(loop) branch = loop.run_until_complete(findbranch(P.mainbranch, P.codepath)) for b in branch: print(b[0], ' => ', b[1])
def test_close_dont_kill_finished(self): async def kill_running(): create = self.loop.subprocess_exec(asyncio.SubprocessProtocol, *PROGRAM_BLOCKED) transport, protocol = await create proc = transport.get_extra_info('subprocess') # kill the process (but asyncio is not notified immediately) proc.kill() proc.wait() proc.kill = mock.Mock() proc_returncode = proc.poll() transport_returncode = transport.get_returncode() transport.close() return (proc_returncode, transport_returncode, proc.kill.called) # Ignore "Unknown child process pid ..." log of SafeChildWatcher, # emitted because the test already consumes the exit status: # proc.wait() with test_utils.disable_logger(): result = self.loop.run_until_complete(kill_running()) test_utils.run_briefly(self.loop) proc_returncode, transport_return_code, killed = result self.assertIsNotNone(proc_returncode) self.assertIsNone(transport_return_code) # transport.close() must not kill the process if it finished, even if # the transport was not notified yet self.assertFalse(killed) # Unlike SafeChildWatcher, FastChildWatcher does not pop the # callbacks if waitpid() is called elsewhere. Let's clear them # manually to avoid a warning when the watcher is detached. if (sys.platform != 'win32' and isinstance(self, SubprocessFastWatcherTests)): asyncio.get_child_watcher()._callbacks.clear()
def setup(self): super().setup() asyncio.get_child_watcher()
def _connect_child(self, argv): if os.name != 'nt': self._child_watcher = asyncio.get_child_watcher() self._child_watcher.attach_loop(self._loop) coroutine = self._loop.subprocess_exec(self._fact, *argv) self._loop.run_until_complete(coroutine)
loop = asyncio.get_event_loop() async def sleep1(): while True: await asyncio.sleep(.2) print('yes') _cmd = """ python -c " import time while True: print('more') time.sleep(1) " """ args = shlex.split(_cmd) async def sleep2(): process = await asyncio.create_subprocess_exec(*args, preexec_fn=os.setpgrp) asyncio.get_child_watcher().attach_loop(loop) # asyncio.ensure_future(sleep2()) loop.call_soon(asyncio.ensure_future, sleep2()) # blocks # loop.run_forever() executor = concurrent.futures.ThreadPoolExecutor() loop.run_in_executor(None, loop.run_forever) print('more')
# schedule tasks for execution asyncio.ensure_future(self.run_cmd_forever(_cmd)) async def run_cmd_forever(self, cmd): print('run cmd forever start') args = shlex.split(cmd) while self.run: # for now do not run like this, just let it die process = await asyncio.create_subprocess_exec(*args) self.processes.append(process) exit_code = await process.wait() for idx, p in enumerate(self.processes): if process.pid == p.pid: self.processes.pop(idx) print("External program '{}' exited with exit code {}, relaunching".format(cmd, exit_code)) asyncio.get_child_watcher() loop = get_event_loop() def cancel_all(): # THIS DOES NOT KILL THE SHELL STUFF! NEED .kill on procs for that! for x in asyncio.Task.all_tasks(): if x == asyncio.Task.current_task(): print('not cancel current task') else: loop.call_soon_threadsafe(x.cancel) async def bleep(x): while True: await asyncio.sleep(1) print('bleep', x)
def start_shell_bleeps(): # you need the child watcher somewhere like here? not sure. it is to do with the shell exec stuff # IMPORTANT IMPORTANT IMPORANT !!!! MUST BE HERE THE LINE BELOW! asyncio.get_child_watcher().attach_loop(loop) loop.call_soon_threadsafe(asyncio.ensure_future, shell_bleep())