Example #1
0
 def __init__(self, loop: AbstractEventLoop):
     super(OKEXAPI, self).__init__()
     self.loop = loop
     self.rest_api = OKEXRESTTradeAPI(loop)
     self.ws_api = OKEXWSTradeAPI(loop)
     self.ws_api.set_msg_handler(self)
     loop.create_task(self.create_session())
Example #2
0
def register(loop: asyncio.AbstractEventLoop, app: web.Application) -> None:
	gateway_sessions = {} # type: Dict[str, GatewaySession]
	app['gateway_sessions'] = gateway_sessions
	app.router.add_route('OPTIONS', '/gateway/gateway.dll', handle_http_gateway_options)
	app.router.add_post('/gateway/gateway.dll', handle_http_gateway)
	
	loop.create_task(_clean_gateway_sessions(gateway_sessions))
    async def test_paginate_timeout(
        self,
        context: MockContext,
        fetcher: ListPageSource[int],
        event_loop: asyncio.AbstractEventLoop,
        advance_time: ClockAdvancer,
    ) -> None:
        p = InteractivePager.create(cast(Any, context), fetcher)

        assert p.paginating
        event_loop.create_task(p.paginate())
        await advance_time(70)
        assert p.paginating
        await advance_time(125)
        assert not p.paginating
        page = await fetcher.get_page(1)
        assert p.embed.description == '\n'.join([
            page['entry_text'],
            '',
            'Confused? React with \N{INFORMATION SOURCE} for more info.',
        ])
        assert not hasattr(p.embed.footer.text, 'text')
        context.channel.send.assert_awaited_once_with(embed=p.embed)
        assert isinstance(p.message, MockMessage)
        assert p.message.add_reaction.await_count == 7
        context.message.clear_reactions.assert_not_awaited()
        p.message.clear_reactions.assert_awaited_once_with()
        p.message.remove_reaction.assert_not_awaited()
        assert p.match is None
Example #4
0
    async def connect(
        self,
        loop: asyncio.AbstractEventLoop = asyncio.get_event_loop()) -> None:
        while True:
            try:
                conn = await websockets.connect(
                    self.url_socket, extra_headers={"Token": self.token})
                self.socket = ackWebsockets.Socket(conn)
                await self.on_connect()

                async def on_disconnect():
                    await self.on_disconnect()
                    await self.reconnect()

                self.socket.onDisconnect(on_disconnect)

                async def on_error(ex: Exception):
                    await self.on_error(ex)
                    await self.reconnect()

                self.socket.onError(on_error)
                self.socket.on("init", self.on_init)
                self.socket.on("stop", self.on_stop)
                self.socket.on_sync("instruction", self.on_instruction)
                loop.create_task(self.socket.run())
                break

            except Exception as e:  # todo: catch concrete exceptions
                await self.on_error(e)
                self.socket = None
                await asyncio.sleep(10)
Example #5
0
 def start(self, loop: asyncio.AbstractEventLoop):
     if hasattr(self, 'started') and self.started:
         # prevent a backend callback from starting more than 1 writer and creating more than 1 queue
         return
     self.queue = Queue()
     loop.create_task(self.writer())
     self.started = True
Example #6
0
def start(loop: asyncio.AbstractEventLoop,
          framerate: int = DEFAULT_FRAMERATE) -> asyncio.Future:
    return asyncio.gather(
        loop.create_task(_ui_refresh(loop, framerate)),
        loop.create_task(_process_inputs(loop)),
        loop=loop,
    )
Example #7
0
def setup(dp: Dispatcher, loop: AbstractEventLoop = None, *args, **kwargs):
    logging.info('Initialize default module')

    loop.create_task(
        forever_run(
            function=check_connection,
            interval=CONNECTION_CHECKER_INTERVAL,
        ))

    dp.register_message_handler(cmd_start, Command('start'), state='*')
    dp.register_message_handler(cmd_help, Command('help'), state='*')
    dp.register_message_handler(cmd_cancel, Command('cancel'), state='*')
    dp.register_message_handler(
        cmd_start,
        lambda message:
        [user for user in message.new_chat_members if bot_id == user.id],
        content_types=ContentType.NEW_CHAT_MEMBERS,
    )
    dp.register_message_handler(
        cmd_start,
        content_types=ContentType.GROUP_CHAT_CREATED,
    )
    dp.register_callback_query_handler(
        answer_callback_empty_button_handler,
        lambda query: query.data == 'None',
    )
    async def test_paginate_next_checked(
        self,
        context: MockContext,
        fetcher: ListPageSource[int],
        event_loop: asyncio.AbstractEventLoop,
        advance_time: ClockAdvancer,
    ) -> None:
        p = InteractivePager.create(cast(Any, context), fetcher)

        event_loop.create_task(p.paginate())
        await advance_time(0)
        reaction = MockReaction.create(
            '\N{BLACK RIGHT-POINTING DOUBLE TRIANGLE WITH VERTICAL BAR}',
            p.message.id)
        await context.bot._dispatch_wait_for('reaction_add', reaction,
                                             context.author)
        await advance_time(5)
        reaction = MockReaction.create('\N{BLACK RIGHT-POINTING TRIANGLE}',
                                       p.message.id)
        await context.bot._dispatch_wait_for('reaction_add', reaction,
                                             context.author)
        await advance_time(5)
        assert p.paginating
        assert p.current_page == 3
        assert isinstance(p.message, MockMessage)
        assert p.message.edit.await_count == 1
        await advance_time(125)
async def run_app(
    loop: asyncio.AbstractEventLoop,
    payload: List[dict],
    entity: str,
    mock_url: str,
) -> None:
    asyncio.set_event_loop(loop)
    with aioresponses() as mocked:
        body = ndjson.dumps(payload)
        mocked.get(mock_url, status=200, body=body)
        test_app = init_app(
            loop=loop,
            settings=settings,
            command_line_args=argparse.Namespace(verbose=False))

        if entity == "patients":
            task = loop.create_task(test_app.resolve_patients())
        elif entity == "encounters":
            task = loop.create_task(test_app.resolve_encounters())
        elif entity == "procedures":
            task = loop.create_task(test_app.resolve_procedures())
        elif entity == "observations":
            task = loop.create_task(test_app.resolve_observations())
        else:
            raise ValueError("unknown entity")

        await asyncio.sleep(SLEEP_PERIOD)
        task.cancel()
        await asyncio.gather(task, return_exceptions=True)
Example #10
0
def test_catch_broken_queue_error(event_loop: AbstractEventLoop) -> None:
    """test for deleting log queue when read queue is awaiting streaming data from log queue"""
    logstreamer = ToyLogStreamer([log_streamer.QueueHandler()])
    read_streamer = LocalReadStreamer(logstreamer)

    async def flow(read_streamer: LocalReadStreamer):
        id = "xxx"
        key = "key-something"
        # add pipe between log streamer and application read streamer
        await read_streamer.add_client(id, key)
        assert read_streamer._id_to_logqueue.get(id)
        assert read_streamer._key_to_readqueue.get(key)

        with pytest.raises(ValueError):
            # keep awaitting to read streamer get log until timeout
            await read_streamer.get(key)

            # successfully raise error and break all running asynchronous process

    async def post_flow(logstreamer):
        await asyncio.sleep(0.3)
        # force delete log queue
        logstreamer.delete("xxx")
        await asyncio.sleep(0.3)

    event_loop.create_task(flow(read_streamer))
    event_loop.run_until_complete(post_flow(logstreamer))
    event_loop.close()
Example #11
0
 def _schedule_stop(
     self,
     exception: Optional[Exception] = None,
     loop: asyncio.AbstractEventLoop = None,
 ):
     loop = self.event_loop if loop is None else loop
     loop.create_task(self.stop(exception=exception))
Example #12
0
    async def setup_liveness(self, loop: asyncio.AbstractEventLoop):
        async def advance_liveness_time(liveness):
            while True:
                await liveness.advance_time(round(time.time()))
                await asyncio.sleep(1)

        await self.liveness_recorder.start()
        loop.create_task(advance_liveness_time(self.liveness_recorder))
Example #13
0
File: ui.py Project: lopter/lightsd
def start(
    loop: asyncio.AbstractEventLoop, framerate: int = DEFAULT_FRAMERATE
) -> asyncio.Future:
    return asyncio.gather(
        loop.create_task(_ui_refresh(loop, framerate)),
        loop.create_task(_process_inputs(loop)),
        loop=loop,
    )
async def start_client(loop: asyncio.AbstractEventLoop = None):
    if loop:
        client = CivDiscordNotifyBotClient(loop=loop)
    else:
        loop = asyncio.get_event_loop()
    token = os.getenv("CIV6_NOTIFY_DISCORD_TOKEN")
    print("Connecting...")
    loop.create_task(client.start(token))
Example #15
0
def create_tasks(
    loop: asyncio.AbstractEventLoop,
    lifecycle: Optional[Callable] = None,
    registry: Optional[registries.BaseRegistry] = None,
    standalone: bool = False,
    priority: int = 0,
    peering_name: str = peering.PEERING_DEFAULT_NAME,
    namespace: Optional[str] = None,
):
    """
    Create all the tasks needed to run the operator, but do not spawn/start them.
    The tasks are properly inter-connected depending on the runtime specification.
    They can be injected into any event loop as needed.
    """

    # The freezer and the registry are scoped to this whole task-set, to sync them all.
    lifecycle = lifecycle if lifecycle is not None else lifecycles.get_default_lifecycle(
    )
    registry = registry if registry is not None else registries.get_default_registry(
    )
    freeze = asyncio.Event()
    tasks = []

    # Monitor the peers, unless explicitly disabled.
    ourselves: Optional[peering.Peer] = peering.Peer.detect(
        id=peering.detect_own_id(),
        priority=priority,
        standalone=standalone,
        namespace=namespace,
        name=peering_name,
    )
    if ourselves:
        tasks.extend([
            loop.create_task(peering.peers_keepalive(ourselves=ourselves)),
            loop.create_task(
                watcher(namespace=namespace,
                        resource=ourselves.resource,
                        handler=functools.partial(
                            peering.peers_handler,
                            ourselves=ourselves,
                            freeze=freeze))),  # freeze is set/cleared
        ])

    # Resource event handling, only once for every known resource (de-duplicated).
    for resource in registry.resources:
        tasks.extend([
            loop.create_task(
                watcher(namespace=namespace,
                        resource=resource,
                        handler=functools.partial(
                            handling.custom_object_handler,
                            lifecycle=lifecycle,
                            registry=registry,
                            resource=resource,
                            freeze=freeze))),  # freeze is only checked
        ])

    return tasks
 def _connect_bots(loop: _asyncio.AbstractEventLoop):
     """
     Helper function used to create tasks and run the bots in an async loop.
     """
     dof_bot_run_task = loop.create_task(_connect(dof_bot, _DOF_BOT_TOKEN))
     testing_bot_run_task = loop.create_task(
         _connect(testing_bot, _TESTING_BOT_TOKEN))
     loop.run_until_complete(
         _asyncio.gather(dof_bot_run_task, testing_bot_run_task))
Example #17
0
def init(bot: commands.Bot, loop: asyncio.AbstractEventLoop) -> None:
    async def check_uploads():
        while True:
            for channel in staticconfig.channel_list:
                await check_and_notify(bot, channel)

            await asyncio.sleep(staticconfig.delay_refresh)

    loop.create_task(check_uploads())
Example #18
0
 async def _do_loop(self, loop: AbstractEventLoop):
     while loop.is_running():
         try:
             sent_from, message = self.in_queue.get(timeout=0.1)
             loop.create_task(
                 self._handle_message(message, sent_from, self._state))
         except Empty:
             pass
         await asyncio.sleep(0)
Example #19
0
    def _run_next_serial_plotting(self, loop: asyncio.AbstractEventLoop, queue: str = "default"):
        next_plot_id = None

        for item in self.plots_queue:
            if item["queue"] == queue and item["state"] is PlotState.SUBMITTED and item["parallel"] is False:
                next_plot_id = item["id"]

        if next_plot_id is not None:
            loop.create_task(self._start_plotting(next_plot_id, loop, queue))
Example #20
0
 def connection_lost_handler(self, loop: AbstractEventLoop,
                             context: Dict[str, Any]) -> None:
     exception = context.get('exception')
     if isinstance(exception, exceptions.ConnectionLostError):
         logging.warning('Connection lost -- trying to reconnect')
         close_task = loop.create_task(self.stop())
         wait_for(close_task, None)
         loop.create_task(self.start())
     else:
         loop.default_exception_handler(context)
Example #21
0
def pass_command_to_loop(command: str, client: MPDClient, loop: asyncio.AbstractEventLoop):
    """
    Utility function that calls specified command on MPD Client
    in specified EventLoop
    :param command: command to be called
    :param client: an instance of MPDClient
    :param loop: target EventLoop
    :return: None
    """
    loop.create_task(client.send_command(command))
Example #22
0
 def connect(
     self,
     websocket: WebSocket,
     filter_id: Optional[UUID] = None,
     loop: asyncio.AbstractEventLoop = asyncio.get_event_loop(),
 ) -> UUID:
     connection_id = super().connect(websocket, filter_id=filter_id, loop=loop)
     if filter_id is not None and filter_id in self._last_events:
         loop.create_task(websocket.send_json(self._last_events[filter_id]))
     return connection_id
Example #23
0
    def start_listening(self, loop: asyncio.AbstractEventLoop = None):
        """
        Attaches the server to an existing event loop, which presumably will later have
        loop.run_forever() called with other things attached to it.

        :param asyncio.AbstractEventLoop loop: the loop to attach
        """
        loop = loop or asyncio.get_event_loop()
        loop.create_task(self.__listen_for_connection())
        self.loop = loop
Example #24
0
async def spawn_app(
    app: ASGIFramework,
    loop: asyncio.AbstractEventLoop,
    config: Config,
    scope: dict,
    send: Callable[[dict], Awaitable[None]],
) -> Callable[[dict], Awaitable[None]]:
    app_queue: asyncio.Queue = asyncio.Queue(config.max_app_queue_size)
    loop.create_task(_handle(app, config, scope, app_queue.get, send))
    return app_queue.put
Example #25
0
def ScheduleTelemetryRequest(loop: asyncio.AbstractEventLoop,
                             Queue: asyncio.Queue, Module, delay: float):
    print('Scheduling new telemtry request on event loop')
    Msg = {
        'InterfaceType': Module['InterfaceType'],
        'MessageType': 'ModuleCommand',
        'Address': Module['Address'],
        'FunctionCode': config.REQ_TEL
    }
    loop.create_task(ScheduleMessage(delay, Queue, Msg))
Example #26
0
async def get_audio_file_range(loop: AbstractEventLoop):
    tasks = []
    dtasks = []
    for n in range(102, 120):
        tasks.append((loop.create_task(get_html(n)), n))
    for task, n in tasks:
        html = await task
        audlink = get_audiolink(html, n)
        dtasks.append(loop.create_task(download_file(n, audlink)))
    for task in dtasks:
        await task
Example #27
0
def init(bot: commands.Bot, loop: asyncio.AbstractEventLoop):
    async def check_ff20():
        vandiland: discord.Guild = bot.get_guild(staticconfig.Vandiland.gid)
        emoji_kekban_emoji: discord.Emoji = await vandiland.fetch_emoji(staticconfig.Vandiland.emoji_kekban)
        forums_channel: discord.TextChannel = vandiland.get_channel(staticconfig.Vandiland.forums_channel_id)

        while True:
            await check_league_incidents_vandiland(forums_channel, emoji_kekban_emoji)
            await asyncio.sleep(staticconfig.delay_refresh * 4)

    loop.create_task(check_ff20())
async def main(loop: asyncio.AbstractEventLoop):
    # 컨디션 생성
    condition = asyncio.Condition()

    # 컨디션을 감시하는 작업 설정
    consumers = [consumer(condition, i) for i in range(5)]

    # 컨디션 변수를 처리하기 위한 작업 예약
    loop.create_task(manipulate_condition(condition))

    # consumer들의 완료를 대기
    await asyncio.wait(consumers)
Example #29
0
async def _accept_loop(server_sock, read_loop_fn,
                       ev_loop: asyncio.AbstractEventLoop,
                       protocol_class: Type[BaseProtocol], protocol_args: list,
                       protocol_kwargs: dict):
    while True:
        client_sock, _addr = await ev_loop.sock_accept(server_sock)
        proto = protocol_class(*protocol_args, **protocol_kwargs)
        peer_name = client_sock.getpeername()
        LOG.debug("Incoming from %s", peer_name)
        proto.on_connected(peer_name)
        ev_loop.create_task(
            read_loop_fn(proto=proto, sock=client_sock, ev_loop=ev_loop))
Example #30
0
async def async_iterator(application: Gtk.Application,
                         loop: asyncio.AbstractEventLoop) -> None:
    while Gtk.events_pending():
        Gtk.main_iteration_do(False)

    await asyncio.sleep(0.01)

    if application.main_window and application.main_window.get_realized():
        loop.create_task(async_iterator(application, loop))
    else:
        application.quit()
        loop.stop()
Example #31
0
 def __init__(
     self,
     input: asyncio.StreamReader,
     output: asyncio.StreamWriter,
     loop: asyncio.AbstractEventLoop,
 ) -> None:
     super().__init__()
     self._input: asyncio.StreamReader = input
     self._output: asyncio.StreamWriter = output
     self.loop: asyncio.AbstractEventLoop = loop
     self.on_message = lambda _: None
     loop.create_task(self._run())
Example #32
0
def waitForEvent(emitter: EventEmitter, eventName: str,  # noqa: C901
                 predicate: Callable[[Any], bool], timeout: float,
                 loop: asyncio.AbstractEventLoop) -> Awaitable:
    """Wait for an event emitted from the emitter."""
    promise = loop.create_future()

    def resolveCallback(target: Any) -> None:
        promise.set_result(target)

    def rejectCallback(exception: Exception) -> None:
        promise.set_exception(exception)

    async def timeoutTimer() -> None:
        await asyncio.sleep(timeout / 1000)
        rejectCallback(
            TimeoutError('Timeout exceeded while waiting for event'))

    def _listener(target: Any) -> None:
        if not predicate(target):
            return
        cleanup()
        resolveCallback(target)

    listener = addEventListener(emitter, eventName, _listener)
    if timeout:
        eventTimeout = loop.create_task(timeoutTimer())

    def cleanup() -> None:
        removeEventListeners([listener])
        if timeout:
            eventTimeout.cancel()

    return promise
Example #33
0
async def total(loop: asyncio.AbstractEventLoop):
    """
    asdf asd f asd f asd f asdf

    .. note:

        asdfasdfas df as df

    >>> a = 1
    >>> int(a)
    1

    @param loop: asdfasdf as df asdf
    :param loop:
    :type loop:
    :return:
    :rtype:
    """
    t = []
    sem = asyncio.Semaphore(10, loop=loop)
    for x in range(1000):
        await sem.acquire()
        t.append(loop.create_task(hello(x, sem)))

    return t
Example #34
0
def run_until_complete(coro: Awaitable, loop: AbstractEventLoop=None):
    """Run a task through to completion.

    The ``.run_until_complete()`` method on asyncio event loop objects does not
    finish tasks when it receives a SIGINT/CTRL-C.  The method simply raises a
    ``KeyboardInterrupt`` exception and this usually results in warnings about
    unfinished tasks plus some "event loop closed" ``RuntimeError`` exceptions
    in pending tasks.

    This is a really annoying default behavior and this function aims at
    replacing that behavior with something that ensures the task actually runs
    through to completion.  When the ``KeyboardInterrupt`` exception is caught,
    the task is canceled and resumed to give it a chance to clean up properly.

    .. versionadded:: 0.4
    .. versionchanged:: 0.5 Can now be called with a ``asyncio.Task`` argument.

    """

    loop = loop or asyncio.get_event_loop()
    if isinstance(coro, asyncio.Task):
        task = coro
    else:
        task = loop.create_task(coro)
    try:
        loop.run_until_complete(task)
    except KeyboardInterrupt:
        task.cancel()
        try:
            loop.run_until_complete(task)
        except asyncio.CancelledError:
            return None
    return task.result()
Example #35
0
async def _scan(targets: Set[str],
                ports: Set[int],
                config: DockerScanModel,
                loop: asyncio.AbstractEventLoop):

    max_concurrency = asyncio.BoundedSemaphore(int(config.concurrency),
                                               loop=loop)

    results = []
    tasks = []

    for target in targets:
        for port in ports:
            await max_concurrency.acquire()

            tasks.append(loop.create_task(_check_ports(
                target,
                port,
                loop,
                max_concurrency,
                results,
                config
            )))

    await asyncio.wait(tasks, loop=loop)

    return results
Example #36
0
    def __init__(
        self,
        name: str,
        offset: Position,
        size: Dimensions,
        loop: asyncio.AbstractEventLoop,
        actions: Dict[UIActionEnum, actions.Action] = None,
    ) -> None:
        self.name = name
        self.size = size
        self.offset = offset
        self.loop = loop
        self.busy = False
        self.children = set()  # type: Set[UIComponent]
        self.actions = actions if actions is not None else {}
        for action in self.actions.values():
            action.set_source(self)
        self.parent = None  # type: UIComponent

        if loop is not None:
            qsize = self.ACTION_QUEUE_SIZE
            self._action_queue = asyncio.Queue(qsize)  # type: asyncio.Queue
            self._action_runner = loop.create_task(self._process_actions())
            self._action_queue_get = None  # type: asyncio.Future
            self._current_action = None  # type: UIActionEnum

        self._nw_corner = offset - Position(1, 1)
        self._se_corner = Position(
            x=self.offset.x + self.size.width - 1,
            y=self.offset.y + self.size.height - 1,
        )
Example #37
0
 async def get_address(self, loop: asyncio.AbstractEventLoop) -> SocketAddress:
     server = ThriftServer(Handler(), port=0)
     serve_task = loop.create_task(server.serve())
     addy = await server.get_address()
     server.stop()
     await serve_task
     return addy
Example #38
0
async def start_lightsd_connection(
    loop: asyncio.AbstractEventLoop,
    lightsd_url: str,
    refresh_delay_s: float = DEFAULT_REFRESH_DELAY,
) -> None:
    global _refresh_task, lightsd

    lightsd = await lightsc.create_async_lightsd_connection(lightsd_url)
    _refresh_task = loop.create_task(_poll(loop, refresh_delay_s))