예제 #1
0
    def _gather(self, *actions: Callable) -> List[Any]:
        g_self = greenlet.getcurrent()
        results: Dict[Callable, Any] = {}
        exceptions: List[Exception] = []

        def action_wrapper(action: Callable) -> Callable:
            def body() -> Any:
                try:
                    results[action] = action()
                except Exception as e:
                    results[action] = e
                    exceptions.append(e)
                g_self.switch()

            return body

        async def task() -> None:
            for action in actions:
                g = greenlet.greenlet(action_wrapper(action))
                g.switch()

        self._loop.create_task(task())

        while len(results) < len(actions):
            self._dispatcher_fiber.switch()

        asyncio._set_running_loop(self._loop)
        if exceptions:
            raise exceptions[0]

        return list(map(lambda action: results[action], actions))
예제 #2
0
async def run_server() -> None:
    async with trio_asyncio.open_loop():
        asyncio._set_running_loop(asyncio.get_event_loop())  # noqa
        config = HyperConfig()
        config.bind = [f"127.0.0.1:5000"]
        config.use_reloader = True
        await serve(app, config)
예제 #3
0
 def value(self) -> T:
     while not self._future.done():
         self._sync_base._dispatcher_fiber.switch()
     asyncio._set_running_loop(self._sync_base._loop)
     if self._exception:
         raise self._exception
     return cast(T, self._value)
예제 #4
0
def _mainwindow():
    app = QtWidgets.QApplication(sys.argv)
    loop = quamash.QEventLoop(app)
    asyncio.set_event_loop(loop)
    asyncio._set_running_loop(loop)
    with loop:
        obj = mainwindow.CMainWindow()
        QtWidgets.QApplication.setStyle(
            QtWidgets.QStyleFactory.create("Fusion"))
        palette = obj.palette()
        dPaletteInfo = {
            QtGui.QPalette.Base: (60, 58, 56),
            QtGui.QPalette.AlternateBase: (80, 80, 80),
            QtGui.QPalette.Window: (56, 56, 56),
            QtGui.QPalette.Text: (180, 180, 180),
            QtGui.QPalette.WindowText: (180, 180, 180),
            QtGui.QPalette.Button: (80, 80, 80),
            QtGui.QPalette.ButtonText: (180, 180, 180),
            QtGui.QPalette.Light: (80, 80, 80),
            QtGui.QPalette.Inactive: (150, 150, 150),
            QtGui.QPalette.Highlight: (150, 150, 150),
        }
        for oQT, tColor in dPaletteInfo.items():
            palette.setColor(oQT, QtGui.QColor(*tColor))
        obj.setPalette(palette)
        obj.show()
        loop.run_forever()
예제 #5
0
파일: util.py 프로젝트: nbashev/noc
 def get_context(self):
     self.prev_loop = asyncio._get_running_loop()
     self.new_loop = asyncio.new_event_loop()
     if self.prev_loop:
         # Reset running loop
         asyncio._set_running_loop(None)
     return self.new_loop
예제 #6
0
    def _start_tcp_server(self, ioloop=None):
        '''Start TCP server to handle remote worker requests.

        Args:

            ioloop (``tornado.IOLoop``, optional):

                If not passed in, Daisy will start an ioloop
                in a concurrent thread
        '''
        self.ioloop = ioloop
        if self.ioloop is None:
            new_event_loop = asyncio.new_event_loop()
            asyncio._set_running_loop(new_event_loop)
            asyncio.set_event_loop(new_event_loop)
            self.ioloop = IOLoop.current()
            t = threading.Thread(target=self.ioloop.start, daemon=True)
            t.start()
        self.tcpserver = DaisyTCPServer()
        self.tcpserver.add_handler(self)
        max_port_tries = 100
        for i in range(max_port_tries):
            try:
                self.tcpserver.listen(0)  # 0 == random port
                break
            except OSError:
                if i == max_port_tries - 1:
                    raise RuntimeError(
                        "Could not find a free port after %d tries " %
                        max_port_tries)
                pass
        self.net_identity = self.tcpserver.get_identity()
예제 #7
0
async def async_main(
    host: str = '127.0.0.1',
    port: int = 5000,
    debug: Optional[bool] = None,
    use_reloader: bool = True,
    ca_certs: Optional[str] = None,
    certfile: Optional[str] = None,
    keyfile: Optional[str] = None,
    **kwargs: Any,
):
    """
    Modified version of Quart's app.run().

    Modification is done to integrate trio_asyncio and Quart together.
    """
    async with trio_asyncio.open_loop() as loop:
        assert loop == asyncio.get_event_loop()
        # this line fix problem with aioredis
        # https://github.com/python-trio/trio-asyncio/issues/63
        asyncio._set_running_loop(asyncio.get_event_loop())

        if kwargs:
            warnings.warn(
                f'Additional arguments, {",".join(kwargs.keys())}, are not supported.\n'
                'They may be supported by Hypercorn, which is the ASGI server Quart '
                'uses by default. This method is meant for development and debugging.'
            )

        scheme = 'https' if certfile is not None and keyfile is not None else 'http'
        print(f'Running on {scheme}://{host}:{port} (CTRL + C to quit)'
              )  # noqa: T001, T002

        await app.run_task(host, port, debug, use_reloader, ca_certs, certfile,
                           keyfile)
예제 #8
0
    def set_is_running(self, value: bool) -> None:
        if value:
            if self._custom_running:
                return

            if not self._context.acquire():
                self._raise_not_owner()

            try:
                # If self._mainloop is already running, we want to acquire self._context to make sure we're on
                # the same thread before checking self._mainloop.is_running().
                self._check_running()
            except:
                self._context.release()
                raise

            self._custom_running = True
            self._old_running_loop = asyncio._get_running_loop()
            asyncio._set_running_loop(self)
        else:
            if self.is_running() and not self._custom_running:
                raise RuntimeError(
                    "Loop was started with run_forever() or run_until_complete()"
                )
            elif not self._custom_running:
                return

            self._context.release()

            asyncio._set_running_loop(self._old_running_loop)
            self._old_running_loop = None
            self._custom_running = False
예제 #9
0
def test_nested_asyncio_with_no_ioloop():
    asyncio.set_event_loop(None)
    try:
        assert some_async_function() == 42
    finally:
        asyncio._set_running_loop(
            None)  # it seems nest_asyncio doesn't reset this
예제 #10
0
 def value(self) -> T:
     while not self._value and not self._exception:
         dispatcher_fiber_.switch()
     asyncio._set_running_loop(self._loop)
     if self._exception:
         raise self._exception
     return cast(T, self._value)
예제 #11
0
    async def _serving_forever(self):
        loop = asyncio.get_event_loop()
        if self._should_serving_fut is not None:
            raise RuntimeError(f"watch pool is already being awaited.")

        if self._start_serving_fut is None:
            raise RuntimeError(
                f"{self._serving_forever} method cannot be called directly.")
        self._should_serving_fut = loop.create_future()
        await self._read_bee()
        try:
            loop = self.loop
            if loop is None:
                loop = asyncio.get_event_loop()
            asyncio._set_running_loop(loop)
            self._start_serving_fut.set_result(self)
            while True:
                self._run_once()
                if self.__stop_running:
                    break
                if self._record_interval_minute:
                    if time.time(
                    ) - self.__starttime > self._record_interval_minute:
                        await self._write_bee()
                        self.__starttime = time.time()
                await asyncio.sleep(0)
        finally:
            self.__stop_running = False
            asyncio._set_running_loop(None)
            self._should_serving_fut = None
예제 #12
0
    def _running_loop(self):
        # The setup and teardown code for run_forever
        # is split off into a contextmanager to be
        # able to reuse this code in GUI applications
        # (see the running_loop context manager
        # in ._helpers.
        old_agen_hooks = sys.get_asyncgen_hooks()
        sys.set_asyncgen_hooks(
            firstiter=self._asyncgen_firstiter_hook,
            finalizer=self._asyncgen_finalizer_hook,
        )
        self._running = True
        asyncio._set_running_loop(self)

        try:
            yield

        finally:
            self._running = False
            asyncio._set_running_loop(None)
            sys.set_asyncgen_hooks(*old_agen_hooks)

        if self._exception is not None:
            exc = self._exception
            self._exception = None
            raise exc
예제 #13
0
 def value(self) -> T:
     while not self._future.done():
         self._sync_base._dispatcher_fiber.switch()
     asyncio._set_running_loop(self._sync_base._loop)
     exception = self._future.exception()
     if exception:
         raise exception
     return cast(T, mapping.from_maybe_impl(self._future.result()))
예제 #14
0
def test_loop_set_is_not_running(loop: GLibEventLoop):
    old_loop = Mock()
    asyncio._set_running_loop(old_loop)

    loop.set_is_running(True)
    loop.set_is_running(False)
    assert not loop.is_running()

    assert asyncio.get_running_loop() is old_loop
예제 #15
0
def test_nested_asyncio_with_existing_ioloop():
    ioloop = asyncio.new_event_loop()
    try:
        asyncio.set_event_loop(ioloop)
        assert some_async_function() == 42
        assert asyncio.get_event_loop() is ioloop
    finally:
        asyncio._set_running_loop(
            None)  # it seems nest_asyncio doesn't reset this
예제 #16
0
async def connect_db():
    asyncio._set_running_loop(asyncio.get_event_loop())
    redis = await trio_asyncio.run_asyncio(
        aioredis.create_redis_pool(
            REDIS_URI,
            password=REDIS_PASSWORD,
            encoding='utf-8',
        ))
    app.db = Database(redis)
예제 #17
0
    def __init__(self):
        super().__init__(selector=LDMudSelector())
        self._thread_id = threading.get_ident()
        self._clock_resolution = 1
        self._sigchld_handler_handle = None
        asyncio._set_running_loop(self)

        ldmud.register_hook(ldmud.ON_HEARTBEAT, self._heart_beat)
        ldmud.register_hook(ldmud.ON_CHILD_PROCESS_TERMINATED,
                            self._signal_handler)
예제 #18
0
 def run_without_glib_until_complete(self, future):
     """
     Run loop without the GLib main loop.  This will block the GLib main
     loop, so only use this for a future that will complete immediately,
     or when the GLib main loop isn't running.
     """
     if asyncio._get_running_loop() is self:
         asyncio._set_running_loop(None)
         super().run_until_complete(future)
         asyncio._set_running_loop(self)
예제 #19
0
async def run_server() -> None:
    async with trio_asyncio.open_loop():
        # trio_asyncio has difficulties with aioredis, workaround here:
        # https://github.com/python-trio/trio-asyncio/issues/63 (answer from @parity3)
        asyncio._set_running_loop(asyncio.get_event_loop())

        config = HyperConfig()
        config.bind = [f"0.0.0.0:5000"]
        config.use_reloader = True
        app.static_folder = "frontend"
        await serve(app, config)
예제 #20
0
    def _sync(self, api_name: str, coro: Awaitable) -> Any:
        __tracebackhide__ = True
        g_self = greenlet.getcurrent()
        task = self._loop.create_task(coro)
        setattr(task, "__pw_api_name__", api_name)
        setattr(task, "__pw_stack_trace__", traceback.extract_stack())

        task.add_done_callback(lambda _: g_self.switch())
        while not task.done():
            self._dispatcher_fiber.switch()
        asyncio._set_running_loop(self._loop)
        return task.result()
예제 #21
0
    def _sync(self, task: asyncio.Future) -> Any:
        g_self = greenlet.getcurrent()
        future = self._loop.create_task(task)

        def callback(result: Any) -> None:
            g_self.switch()

        future.add_done_callback(callback)
        while not future.done():
            self._dispatcher_fiber.switch()
        asyncio._set_running_loop(self._loop)
        return future.result()
예제 #22
0
def get_connected_protocol(app, protocol_cls, event_loop, **kwargs):
    loop = MockLoop(event_loop)
    asyncio._set_running_loop(loop)
    transport = MockTransport()
    config = Config(app=app, **kwargs)
    server_state = ServerState()
    protocol = protocol_cls(config=config, server_state=server_state, _loop=loop)
    protocol.connection_made(transport)
    try:
        yield protocol
    finally:
        protocol.loop.close()
        asyncio._set_running_loop(None)
예제 #23
0
    def _sync(self, coro: Awaitable) -> Any:
        stack_trace = traceback.extract_stack()
        g_self = greenlet.getcurrent()
        task = self._loop.create_task(coro)
        setattr(task, "__pw_stack_trace__", stack_trace)

        def callback(result: Any) -> None:
            g_self.switch()

        task.add_done_callback(callback)
        while not task.done():
            self._dispatcher_fiber.switch()
        asyncio._set_running_loop(self._loop)
        return task.result()
예제 #24
0
    def _run_mainloop(self):
        old_running_loop = asyncio._get_running_loop()
        asyncio._set_running_loop(self)

        try:
            if not self._context.acquire():
                self._raise_not_owner()

            try:
                self._mainloop.run()
            finally:
                self._context.release()
        finally:
            asyncio._set_running_loop(old_running_loop)
예제 #25
0
 def run_forever(self):
     self._running = True
     asyncio._set_running_loop(self)
     while (self._immediate or self._scheduled) and self._running:
         if self._immediate:
             h = self._immediate[0]
             self._immediate = self._immediate[1:]
         else:
             h = heapq.heappop(self._scheduled)
             self._time = h._when
             h._scheduled = False
         if not h._cancelled:
             h._run()
         if self._exc is not None:
             raise self._exc
예제 #26
0
def fsspec_loop():
    """Temporarily switch the current event loop to the fsspec's
    own loop, and then revert it back after the context gets
    terinated.
    """
    try:
        original_loop = get_running_loop()
    except RuntimeError:
        original_loop = None

    fsspec_loop = get_loop()
    try:
        asyncio._set_running_loop(fsspec_loop)
        yield fsspec_loop
    finally:
        asyncio._set_running_loop(original_loop)
예제 #27
0
    def __init__(self, context=None, ioloop=None):
        '''Initialize TCP connection with the scheduler.

        Args:

            context (`class:daisy.Context`, optional):

                If given, will be used to connect to the scheduler. If not
                given, the context will be read from environment variable
                ``DAISY_CONTEXT``.

            ioloop(``tornado.IOLoop``, optional):

                If not passed in, Clientwill start an ioloop in a concurrent
                thread
        '''
        logger.debug("Client init")
        self.context = context
        self.connected = False
        self.error_state = False
        self.stream = None

        if self.context is None:
            self.context = Context.from_env()

        self.ioloop = ioloop
        if self.ioloop is None:
            new_event_loop = asyncio.new_event_loop()
            asyncio._set_running_loop(new_event_loop)
            asyncio.set_event_loop(new_event_loop)
            self.ioloop = IOLoop.current()
            t = threading.Thread(target=self.ioloop.start, daemon=True)
            t.start()

        self.ioloop.add_callback(self._start)

        logger.debug("Waiting for connection to Daisy scheduler...")
        while not self.connected:
            time.sleep(.1)
            if self.error_state:
                logger.error("Cannot connect to Daisy scheduler")
                sys.exit(1)

        self.send(
            SchedulerMessage(SchedulerMessageType.WORKER_HANDSHAKE,
                             data=self.context.worker_id))
예제 #28
0
def test_fsspec_loop():
    asyncio._set_running_loop(None)

    with fsspec.asyn.fsspec_loop() as loop:
        assert get_running_loop() is loop
        assert get_running_loop() is fsspec.asyn.get_loop()

    with pytest.raises(RuntimeError):
        get_running_loop()

    original_loop = asyncio.new_event_loop()
    asyncio._set_running_loop(original_loop)

    with fsspec.asyn.fsspec_loop() as loop:
        assert get_running_loop() is loop
        assert get_running_loop() is fsspec.asyn.get_loop()

    assert get_running_loop() is original_loop
예제 #29
0
def await_(awaitable: typing.Awaitable[T]) -> T:
    # create another event loop within the same thread
    old_loop: typing.Optional[asyncio.AbstractEventLoop]
    try:
        old_loop = asyncio.get_running_loop()
    except RuntimeError:
        old_loop = None
    asyncio._set_running_loop(None)
    loop = asyncio.new_event_loop()
    try:
        return loop.run_until_complete(awaitable)
    finally:
        try:
            loop.run_until_complete(loop.shutdown_asyncgens())
        finally:
            loop.close()
            asyncio.set_event_loop(old_loop)
            asyncio._set_running_loop(old_loop)
예제 #30
0
파일: util.py 프로젝트: nbashev/noc
 def drop_context(self):
     # Cancel all tasks
     to_cancel = asyncio.all_tasks(self.new_loop)
     for task in to_cancel:
         task.cancel()
     asyncio.gather(*to_cancel, loop=self.new_loop, return_exceptions=True)
     self.new_loop.run_until_complete(self.new_loop.shutdown_asyncgens())
     #
     self.new_loop.close()
     self.new_loop = None
     asyncio._set_running_loop(self.prev_loop)
     if self.prev_loop:
         asyncio._set_running_loop(self.prev_loop)
     else:
         asyncio._set_running_loop(None)
         asyncio.get_event_loop_policy().reset_called()
     self.prev_loop = None