Python asyncio.get_event_loop() Examples

The following are 30 code examples of asyncio.get_event_loop(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module asyncio , or try the search function .
Example #1
Source File: utils.py    From controller with MIT License 7 votes vote down vote up
def async_run(tasks):
    """
    run a group of tasks async
    Requires the tasks arg to be a list of functools.partial()
    """
    if not tasks:
        return

    # start a new async event loop
    loop = asyncio.get_event_loop()
    # https://github.com/python/asyncio/issues/258
    executor = concurrent.futures.ThreadPoolExecutor(5)
    loop.set_default_executor(executor)

    async_tasks = [asyncio.ensure_future(async_task(task, loop)) for task in tasks]
    # run tasks in parallel
    loop.run_until_complete(asyncio.wait(async_tasks))
    # deal with errors (exceptions, etc)
    for task in async_tasks:
        error = task.exception()
        if error is not None:
            raise error

    executor.shutdown(wait=True) 
Example #2
Source File: client.py    From gql with MIT License 6 votes vote down vote up
def subscribe(
        self, document: DocumentNode, *args, **kwargs
    ) -> Generator[Dict, None, None]:
        """Execute a GraphQL subscription with a python generator.

        We need an async transport for this functionality.
        """

        async_generator = self.subscribe_async(document, *args, **kwargs)

        loop = asyncio.get_event_loop()

        assert not loop.is_running(), (
            "Cannot run client.subscribe if an asyncio loop is running."
            " Use subscribe_async instead."
        )

        try:
            while True:
                result = loop.run_until_complete(async_generator.__anext__())
                yield result

        except StopAsyncIteration:
            pass 
Example #3
Source File: test_asyncio.py    From hiku with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def db_dsn_fixture(request):
    loop = asyncio.get_event_loop()

    pg_dsn = 'postgresql://postgres:postgres@postgres:5432/postgres'
    db_name = loop.run_until_complete(init_db(pg_dsn, loop=loop))

    db_dsn = 'postgresql://postgres:postgres@postgres:5432/{}'.format(db_name)
    loop.run_until_complete(setup_db(db_dsn, loop=loop))

    def fin():
        loop.run_until_complete(drop_db(pg_dsn, db_name, loop=loop))

    request.addfinalizer(fin)
    return db_dsn

# define graph 
Example #4
Source File: imagenet_api.py    From mlimages with MIT License 6 votes vote down vote up
def gather(self, wnid, relative="", include_subset=False):
        loop = asyncio.get_event_loop()
        session = self.create_session(loop)
        folders = []

        f = loop.run_until_complete(self.download_images(session, wnid, relative))
        folders.append(f)

        if include_subset:
            wnids = self._get_subsets(wnid)
            path = self.file_api.join_relative(relative, f)
            downloads = asyncio.wait([self.download_images(session, wnid, path) for wnid in wnids])
            done, pending = loop.run_until_complete(downloads)
            folders += [d.result() for d in done]

        session.close()

        return folders 
Example #5
Source File: __init__.py    From trader with Apache License 2.0 6 votes vote down vote up
def __init__(self, io_loop: asyncio.AbstractEventLoop = None):
        super().__init__()
        self.io_loop = io_loop or asyncio.get_event_loop()
        self.sub_client = self.io_loop.run_until_complete(
                aioredis.create_redis((config.get('REDIS', 'host', fallback='localhost'),
                                       config.getint('REDIS', 'port', fallback=6379)),
                                      db=config.getint('REDIS', 'db', fallback=1)))
        self.redis_client = redis.StrictRedis(
            host=config.get('REDIS', 'host', fallback='localhost'),
            db=config.getint('REDIS', 'db', fallback=1), decode_responses=True)
        self.initialized = False
        self.sub_tasks = list()
        self.sub_channels = list()
        self.channel_router = dict()
        self.crontab_router = defaultdict(dict)
        self.datetime = None
        self.time = None
        self.loop_time = None 
Example #6
Source File: fetch_data.py    From trader with Apache License 2.0 6 votes vote down vote up
def fetch_bar2():
    day = datetime.datetime.strptime('20160114', '%Y%m%d').replace(tzinfo=pytz.FixedOffset(480))
    end = datetime.datetime.strptime('20160914', '%Y%m%d').replace(tzinfo=pytz.FixedOffset(480))
    while day <= end:
        day, trading = await is_trading_day(day)
        if trading:
            print('process ', day)
            tasks = [
                asyncio.ensure_future(update_from_shfe(day)),
                asyncio.ensure_future(update_from_dce(day)),
                asyncio.ensure_future(update_from_czce(day)),
                asyncio.ensure_future(update_from_cffex(day)),
            ]
            await asyncio.wait(tasks)
        day += datetime.timedelta(days=1)
    print('all done!')


# asyncio.get_event_loop().run_until_complete(fetch_bar2())
# create_main_all()
# fetch_from_quandl_all()
# clean_dailybar()
# load_kt_data() 
Example #7
Source File: main.py    From trader with Apache License 2.0 6 votes vote down vote up
def main():
    loop = asyncio.get_event_loop()
    big_brother = None
    try:
        pid_path = os.path.join(app_dir.user_cache_dir, 'trader.pid')
        if not os.path.exists(pid_path):
            if not os.path.exists(app_dir.user_cache_dir):
                os.makedirs(app_dir.user_cache_dir)
        with open(pid_path, 'w') as pid_file:
            pid_file.write(str(os.getpid()))
        big_brother = TradeStrategy(io_loop=loop)
        print('Big Brother is watching you!')
        print('used config file:', config_file)
        print('log stored in:', app_dir.user_log_dir)
        print('pid file:', pid_path)
        loop.create_task(big_brother.install())
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    except Exception as ee:
        logger.info('发生错误: %s', repr(ee), exc_info=True)
    finally:
        big_brother and loop.run_until_complete(big_brother.uninstall())
        logger.info('程序已退出') 
Example #8
Source File: test_createDefaultHtmlFile.py    From wuy with GNU General Public License v2.0 6 votes vote down vote up
def test():
    class aeff(wuy.Window):
        size = (100, 100)

        def init(self):
            asyncio.get_event_loop().call_later(2, self.exit)

    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # the following line is needed
    # because pytest seems to execute from a different path
    # then the executable one (think freezed)
    # ex: it works without it, in a real context
    # ex: it's needed when pytest execute the test
    # IRL : it's not needed to change the path
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    wuy.PATH = os.getcwd()  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    cf = "web/aeff.html"
    if os.path.isfile(cf):
        os.unlink(cf)
    aeff()
    assert os.path.isfile(cf), "a default file can't be created !!!"
    os.unlink(cf) 
Example #9
Source File: async_mixin.py    From botbuilder-python with MIT License 6 votes vote down vote up
def __anext__(self):
        loop = asyncio.get_event_loop()
        try:
            chunk = await loop.run_in_executor(
                None, _msrest_next, self.iter_content_func
            )
            if not chunk:
                raise _MsrestStopIteration()
            if self.user_callback and callable(self.user_callback):
                self.user_callback(chunk, self.response)
            return chunk
        except _MsrestStopIteration:
            self.response.close()
            raise StopAsyncIteration()
        except Exception as err:
            _LOGGER.warning("Unable to stream download: %s", err)
            self.response.close()
            raise 
Example #10
Source File: _synchronise.py    From quart with MIT License 6 votes vote down vote up
def sync_with_context(future: Awaitable) -> Any:
    context = None
    if _request_ctx_stack.top is not None:
        context = _request_ctx_stack.top.copy()
    elif _websocket_ctx_stack.top is not None:
        context = _websocket_ctx_stack.top.copy()
    elif _app_ctx_stack.top is not None:
        context = _app_ctx_stack.top.copy()

    async def context_wrapper() -> Any:
        if context is not None:
            async with context:
                return await future
        else:
            return await future

    return asyncio.get_event_loop().sync_wait(context_wrapper())  # type: ignore 
Example #11
Source File: get_references_web_single_group.py    From fine-lm with MIT License 6 votes vote down vote up
def main(_):
  urls = get_urls_for_shard_group(
      FLAGS.urls_dir, FLAGS.shard_id, FLAGS.group_id)
  tf.logging.info("Fetching %d URLs for shard %d, group %d",
                  len(urls), FLAGS.shard_id, FLAGS.group_id)

  tf.gfile.MakeDirs(FLAGS.out_dir)
  out_fname = tfrecord_fname(FLAGS.out_dir, FLAGS.shard_id)

  with utils.timing("group_fetch"):
    logging_fnames = {}
    if FLAGS.log_samples:
      logging_fnames["samples"] = os.path.join(
          FLAGS.out_dir, "samples.%d.txt" % FLAGS.shard_id)
    loop = asyncio.get_event_loop()
    num_written = loop.run_until_complete(asyncio.ensure_future(
        fetch_urls(urls,
                   out_fname,
                   logging_fnames)))

  tf.logging.info("Total URLs: %d", len(urls))
  tf.logging.info("Num written: %d", num_written)
  tf.logging.info("Coverage: %.1f", (num_written / len(urls)) * 100) 
Example #12
Source File: progress_bar.py    From quart with MIT License 6 votes vote down vote up
def start_work():
    global aredis
    loop = asyncio.get_event_loop()
    aredis = await aioredis.create_redis('redis://localhost', loop=loop)

    if await aredis.get('state') == b'running':
        return "<center>Please wait for current work to finish.</center>"
    else:
        await aredis.set('state', 'ready')

    if await aredis.get('state') == b'ready':
        loop.create_task(some_work())
        body = '''
        <center>
        work started!
        </center>
        <script type="text/javascript">
            window.location = "''' + url_for('progress') + '''";
        </script>'''
        return body 
Example #13
Source File: statsd.py    From gnocchi with Apache License 2.0 5 votes vote down vote up
def start():
    conf = service.prepare_service()

    if conf.statsd.resource_id is None:
        raise cfg.RequiredOptError("resource_id", cfg.OptGroup("statsd"))

    stats = Stats(conf)

    loop = asyncio.get_event_loop()
    # TODO(jd) Add TCP support
    listen = loop.create_datagram_endpoint(
        lambda: StatsdServer(stats),
        local_addr=(conf.statsd.host, conf.statsd.port))

    def _flush():
        loop.call_later(conf.statsd.flush_delay, _flush)
        stats.flush()

    loop.call_later(conf.statsd.flush_delay, _flush)
    transport, protocol = loop.run_until_complete(listen)

    LOG.info("Started on %s:%d", conf.statsd.host, conf.statsd.port)
    LOG.info("Flush delay: %d seconds", conf.statsd.flush_delay)

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    transport.close()
    loop.close() 
Example #14
Source File: aio.py    From python-consul2 with MIT License 5 votes vote down vote up
def __init__(self, *args, loop=None, **kwargs):
        super(HTTPClient, self).__init__(*args, **kwargs)
        self._loop = loop or asyncio.get_event_loop() 
Example #15
Source File: child.py    From hacking-tools with MIT License 5 votes vote down vote up
def __init__(self,id,url):
        self.id = id+"-"+REQ_KEY
        # print(self.id)
        # self.connection = redis.Connection()
        self.url = url
        self.loop = asyncio.get_event_loop()
        self.client = aiohttp.ClientSession(loop=self.loop)
        self.count = 0 # total request/s 
Example #16
Source File: async_scan.py    From Vxscan with Apache License 2.0 5 votes vote down vote up
def pool(self):
        loop = asyncio.get_event_loop()
        future = asyncio.ensure_future(self.run(self.host))
        loop.run_until_complete(future)
        
        self.save(self.outjson) 
Example #17
Source File: test_signal_handlers.py    From sanic with MIT License 5 votes vote down vote up
def test_windows_workaround():
    """Test Windows workaround (on any other OS)"""
    # At least some code coverage, even though this test doesn't work on
    # Windows...
    class MockApp:
        def __init__(self):
            self.is_stopping = False

        def stop(self):
            assert not self.is_stopping
            self.is_stopping = True

        def add_task(self, func):
            loop = asyncio.get_event_loop()
            self.stay_active_task = loop.create_task(func(self))

    async def atest(stop_first):
        app = MockApp()
        ctrlc_workaround_for_windows(app)
        await asyncio.sleep(0.05)
        if stop_first:
            app.stop()
            await asyncio.sleep(0.2)
        assert app.is_stopping == stop_first
        # First Ctrl+C: should call app.stop() within 0.1 seconds
        os.kill(os.getpid(), signal.SIGINT)
        await asyncio.sleep(0.2)
        assert app.is_stopping
        assert app.stay_active_task.result() == None
        # Second Ctrl+C should raise
        with pytest.raises(KeyboardInterrupt):
            os.kill(os.getpid(), signal.SIGINT)
        return "OK"

    # Run in our private loop
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    res = loop.run_until_complete(atest(False))
    assert res == "OK"
    res = loop.run_until_complete(atest(True))
    assert res == "OK" 
Example #18
Source File: test_keep_alive_timeout.py    From sanic with MIT License 5 votes vote down vote up
def __init__(self, app, loop=None):
        super().__init__(app)
        if loop is None:
            loop = asyncio.get_event_loop()
        self._loop = loop
        self._server = None
        self._tcp_connector = None
        self._session = None 
Example #19
Source File: web_helper.py    From JJMumbleBot with GNU General Public License v3.0 5 votes vote down vote up
def initialize_web():
    ws = websockets.serve(send_message, global_settings.cfg[C_WEB_SETTINGS][P_WEB_IP], int(global_settings.cfg[C_WEB_SETTINGS][P_WEB_SOCK_PORT]),
                          origins=None)
    asyncio.get_event_loop().run_until_complete(ws)
    global_settings.socket_server = Thread(target=asyncio.get_event_loop().run_forever, daemon=True)
    global_settings.socket_server.start()
    rprint("Initialized Socket Server.", origin=L_WEB_INTERFACE)
    log(INFO, "Initialized Socket Server", origin=L_WEB_INTERFACE)

    global_settings.flask_server = Thread(target=start_flask_server, daemon=True)
    global_settings.flask_server.start()
    rprint("Initialized Flask Server.", origin=L_WEB_INTERFACE)
    log(INFO, "Initialized Flask Server", origin=L_WEB_INTERFACE) 
Example #20
Source File: app.py    From sarlacc with MIT License 5 votes vote down vote up
def main():
    # Read config
    config = ConfigParser()
    config.readfp(open(os.path.dirname(os.path.abspath(__file__)) + "/smtpd.cfg.default"))
    config.read(["smtpd.cfg",])

    # Configure the logger
    logging.basicConfig(level=getattr(logging, config["logging"]["log_level"].upper()),
            format='%(levelname)s: %(asctime)s %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p')

    loop = asyncio.get_event_loop()

    # Init plugin manager
    plugin_manager = PluginManager(loop)

    logger.info("Starting smtpd on {}:{}".format(config["smtpd"]["host"], config["smtpd"]["port"]))
    cont = CustomIdentController(
            MailHandler(loop, config, plugin_manager),
            loop=loop,
            ident_hostname=config["smtpd"]["hostname"],
            ident=config["smtpd"]["ident"],
            hostname=config["smtpd"]["host"],
            port=config["smtpd"]["port"])
    cont.start()

    # Ugly but whatever, wait until the controller thread finishes (wtf why do they start a thread)
    threads = threading.enumerate()
    for thread in threads:
        if not threading.current_thread() == thread:
            thread.join()

    plugin_manager.stop_plugins() 
Example #21
Source File: threading_helper.py    From botbuilder-python with MIT License 5 votes vote down vote up
def get_event_loop():
    global _loop_thread
    with _lock:
        if _loop_thread is None:
            _loop_thread = EventLoopThread()
            _loop_thread.start()
        return _loop_thread.loop 
Example #22
Source File: test_conversations_async.py    From botbuilder-python with MIT License 5 votes vote down vote up
def __init__(self, method_name):
        super(TestAsyncConversation, self).__init__(method_name)
        self.loop = asyncio.get_event_loop()
        self.credentials = MicrosoftTokenAuthenticationStub(AUTH_TOKEN) 
Example #23
Source File: test_attachments_async.py    From botbuilder-python with MIT License 5 votes vote down vote up
def __init__(self, method_name):
        super(AttachmentsTest, self).__init__(method_name)
        self.loop = asyncio.get_event_loop() 
Example #24
Source File: bot_app.py    From botbuilder-python with MIT License 5 votes vote down vote up
def __init__(self):
        # Create the loop and Flask app
        self.loop = asyncio.get_event_loop()
        self.flask = Flask(__name__, instance_relative_config=True)
        self.flask.config.from_object(DefaultConfig)

        # Create adapter.
        # See https://aka.ms/about-bot-adapter to learn more about how bots work.
        self.settings = BotFrameworkAdapterSettings(
            self.flask.config["APP_ID"], self.flask.config["APP_PASSWORD"]
        )
        self.adapter = BotFrameworkAdapter(self.settings)

        # Catch-all for errors.
        async def on_error(adapter, context: TurnContext, error: Exception):
            # This check writes out errors to console log .vs. app insights.
            # NOTE: In production environment, you should consider logging this to Azure
            #       application insights.
            print(f"\n [on_turn_error]: {error}", file=sys.stderr)

            # Send a message to the user
            error_message_text = "Sorry, it looks like something went wrong."
            error_message = MessageFactory.text(
                error_message_text, error_message_text, InputHints.expecting_input
            )
            await context.send_activity(error_message)

            # pylint: disable=protected-access
            if adapter._conversation_state:
                # If state was defined, clear it.
                await adapter._conversation_state.delete(context)

        self.adapter.on_turn_error = MethodType(on_error, self.adapter)

        # Create the main dialog
        self.bot = MyBot() 
Example #25
Source File: local.py    From quart with MIT License 5 votes vote down vote up
def _task_identity() -> int:
        loop = asyncio.get_event_loop()
        if loop.is_running():
            task = asyncio.current_task()
            task_id = id(task)
            return task_id
        else:
            return 0 
Example #26
Source File: asyncio.py    From hiku with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, loop=None):
        self._loop = loop or get_event_loop() 
Example #27
Source File: serial_asyncio.py    From kivy-smoothie-host with GNU General Public License v3.0 5 votes vote down vote up
def open_serial_connection(*,
                           loop=None,
                           limit=asyncio.streams._DEFAULT_LIMIT,
                           **kwargs):
    """A wrapper for create_serial_connection() returning a (reader,
    writer) pair.

    The reader returned is a StreamReader instance; the writer is a
    StreamWriter instance.

    The arguments are all the usual arguments to Serial(). Additional
    optional keyword arguments are loop (to set the event loop instance
    to use) and limit (to set the buffer limit passed to the
    StreamReader.

    This function is a coroutine.
    """
    if loop is None:
        loop = asyncio.get_event_loop()
    reader = asyncio.StreamReader(limit=limit, loop=loop)
    protocol = asyncio.StreamReaderProtocol(reader, loop=loop)
    transport, _ = yield from create_serial_connection(
        loop=loop,
        protocol_factory=lambda: protocol,
        **kwargs)
    writer = asyncio.StreamWriter(transport, protocol, reader, loop)
    return reader, writer


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# test 
Example #28
Source File: test_hachiko.py    From hachiko with MIT License 5 votes vote down vote up
def test_hachiko_aiowatchdog(tmpdir, capsys):
    """Test hachiko AIOWatchdog watcher."""
    WATCH_DIRECTORY = str(tmpdir)
    async def watch_fs():
        event_handler = SubclassEventHandler()  # hachinko style event handler
        watcher = AIOWatchdog(WATCH_DIRECTORY, event_handler=event_handler)
        watcher.start()
        await check_output_is_expected(WATCH_DIRECTORY, capsys)
        # Stop the directory watcher
        watcher.stop()
    # Start the event loop
    asyncio.get_event_loop().run_until_complete(watch_fs()) 
Example #29
Source File: test_hachiko.py    From hachiko with MIT License 5 votes vote down vote up
def test_hachiko_with_watchdog(tmpdir, capsys):
    """Test hachiko with a regular watchdog observer."""
    WATCH_DIRECTORY = str(tmpdir)
    async def watch_fs():
        event_handler = SubclassEventHandler()  # hachinko style event handler
        observer = watchdog.observers.Observer()  # a regular watchdog observer
        observer.schedule(event_handler, WATCH_DIRECTORY, recursive=True)
        observer.start()
        await check_output_is_expected(WATCH_DIRECTORY, capsys)
        # Stop the directory watcher
        observer.stop()
    # Start the event loop
    asyncio.get_event_loop().run_until_complete(watch_fs()) 
Example #30
Source File: test_app.py    From sanic with MIT License 5 votes vote down vote up
def test_create_asyncio_server(app):
    if not uvloop_installed():
        loop = asyncio.get_event_loop()
        asyncio_srv_coro = app.create_server(return_asyncio_server=True)
        assert isawaitable(asyncio_srv_coro)
        srv = loop.run_until_complete(asyncio_srv_coro)
        assert srv.is_serving() is True