def add_child_handler(self, pid, callback, *args): loop = events.get_running_loop() thread = threading.Thread( target=self._do_waitpid, name=f"waitpid-{next(self._pid_counter)}", args=(loop, pid, callback, args), daemon=True, ) self._threads[pid] = thread thread.start()
def _smol_save(nvim: Nvim) -> None: global _handle if _handle: _handle.cancel() def cont() -> None: go(async_call(nvim, nvim.command, "silent! wa!")) loop = get_running_loop() _handle = loop.call_later(0.5, cont)
def _get_running_loop(): frame = sys._getframe(1) if frame.f_globals['__name__'] == 'asyncio.mixins': # When we called from LoopBoundedMixin we should # fallback to default implementation of get_running_loop try: return events.get_running_loop() except RuntimeError: return None return None
async def _listen_to(self, channel): loop = get_running_loop() loop.add_reader(sys.stdin, lambda: self.queue.put_nowait(sys.stdin.readline())) while True: msg = await self.queue.get() self.handle_msg( IncomingMessage(message=msg, channel=channel, date=datetime.now(tz=timezone.utc), **_BASE_MSG))
async def to_thread(func, *args, **kwargs): """Asynchronously run function *func* in a separate thread. Any *args and **kwargs supplied for this function are directly passed to *func*. Also, the current :class:`contextvars.Context` is propagated, allowing context variables from the main thread to be accessed in the separate thread. Return a coroutine that can be awaited to get the eventual result of *func*. """ loop = events.get_running_loop() ctx = contextvars.copy_context() func_call = functools.partial(ctx.run, func, *args, **kwargs) return await loop.run_in_executor(None, func_call)
async def wait(self): waiter = get_running_loop().create_future() self.waiters.put(waiter) await waiter
"""High-level support for working with threads in asyncio""" import functools import contextvars from asyncio import events async def toThread(func, /, *args, **kwargs): """Asynchronously run function *func* in a separate thread. Any *args and **kwargs supplied for this function are directly passed to *func*. Also, the current :class:`contextvars.Context` is propagated, allowing context variables from the main thread to be accessed in the separate thread. Return a coroutine that can be awaited to get the eventual result of *func*. """ loop = events.get_running_loop() ctx = contextvars.copy_context() func_call = functools.partial(ctx.run, func, *args, **kwargs) return await loop.run_in_executor(None, func_call)