Python asyncio.set_event_loop() Examples
The following are 30
code examples of asyncio.set_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: vlcscheduler.py From VLC-Scheduler with MIT License | 8 votes |
def main(): logger.info('VLC Scheduler v%s started.' % version.VERSION) if sys.platform == 'win32': loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop) loop = asyncio.get_event_loop() try: loop.run_until_complete(main_coro()) except Exception as e: if config.DEBUG: logger.fatal(traceback.format_exc()) else: logger.fatal(str(e)) finally: loop.close() logger.info('VLC Scheduler stopped.')
Example #2
Source File: test_logo.py From sanic with MIT License | 7 votes |
def test_logo_false(app, caplog): app.config.LOGO = False server = app.create_server( debug=True, return_asyncio_server=True, port=PORT ) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop._stopping = False with caplog.at_level(logging.DEBUG): _server = loop.run_until_complete(server) _server.close() loop.run_until_complete(_server.wait_closed()) app.stop() banner, port = caplog.record_tuples[ROW][2].rsplit(":", 1) assert caplog.record_tuples[ROW][1] == logging.INFO assert banner == "Goin' Fast @ http://127.0.0.1" assert int(port) > 0
Example #3
Source File: test_logo.py From sanic with MIT License | 6 votes |
def test_logo_custom(app, caplog): app.config.LOGO = "My Custom Logo" server = app.create_server( debug=True, return_asyncio_server=True, port=PORT ) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop._stopping = False with caplog.at_level(logging.DEBUG): _server = loop.run_until_complete(server) _server.close() loop.run_until_complete(_server.wait_closed()) app.stop() assert caplog.record_tuples[ROW][1] == logging.DEBUG assert caplog.record_tuples[ROW][2] == "My Custom Logo"
Example #4
Source File: support.py From molotov with Apache License 2.0 | 6 votes |
def dedicatedloop_noclose(func): @functools.wraps(func) def _loop(*args, **kw): old_loop = asyncio.get_event_loop() loop = asyncio.new_event_loop() loop.set_debug(True) loop._close = loop.close loop.close = lambda: None asyncio.set_event_loop(loop) try: return func(*args, **kw) finally: loop._close() asyncio.set_event_loop(old_loop) return _loop
Example #5
Source File: post_rotation.py From Penny-Dreadful-Tools with GNU General Public License v3.0 | 6 votes |
def ad_hoc() -> None: try: event_loop = asyncio.get_event_loop() except RuntimeError: event_loop = asyncio.new_event_loop() asyncio.set_event_loop(event_loop) league.set_status(league.Status.CLOSED) multiverse.init() # New Cards? event_loop.run_until_complete(multiverse.set_legal_cards_async()) # PD current list event_loop.run_until_complete(multiverse.update_pd_legality_async()) # PD previous lists insert_seasons.run() # Make sure Season table is up to date if redis.REDIS: # Clear the redis cache redis.REDIS.flushdb() league_end = league.active_league().end_date diff = league_end - dtutil.now() if diff.days > 0: league.set_status(league.Status.OPEN) print('Open the gates here') reprime_cache.run() # Update deck legalities if redis.REDIS: # Clear the redis cache redis.REDIS.flushdb()
Example #6
Source File: asyncio_common.py From moler with BSD 3-Clause "New" or "Revised" License | 6 votes |
def run_via_asyncio(async_to_run, debug_event_loop=False): logger = logging.getLogger('asyncio.main') asyncio.set_event_loop(asyncio.new_event_loop()) event_loop = asyncio.get_event_loop() event_loop.set_debug(enabled=debug_event_loop) try: logger.info("starting events loop ...") event_loop.run_until_complete(async_to_run) _cleanup_remaining_tasks(loop=event_loop, logger=logger) finally: logger.info("closing events loop ...") event_loop.close() logger.info("... events loop closed")
Example #7
Source File: unix_ls_on_device_async.py From moler with BSD 3-Clause "New" or "Revised" License | 6 votes |
def run_via_asyncio(async_to_run, debug_event_loop=False): logger = logging.getLogger('asyncio.main') asyncio.set_event_loop(asyncio.new_event_loop()) event_loop = asyncio.get_event_loop() event_loop.set_debug(enabled=debug_event_loop) try: logger.info("starting events loop ...") event_loop.run_until_complete(async_to_run) _cleanup_remaining_tasks(loop=event_loop, logger=logger) finally: logger.info("closing events loop ...") event_loop.close() logger.info("... events loop closed") # configure library directly from dict
Example #8
Source File: main.py From friendly-telegram with GNU Affero General Public License v3.0 | 6 votes |
def parse_arguments(): """Parse the arguments""" parser = argparse.ArgumentParser() parser.add_argument("--setup", "-s", action="store_true") parser.add_argument("--phone", "-p", action="append") parser.add_argument("--token", "-t", action="append", dest="tokens") parser.add_argument("--heroku", action="store_true") parser.add_argument("--local-db", dest="local", action="store_true") parser.add_argument("--web-only", dest="web_only", action="store_true") parser.add_argument("--no-web", dest="web", action="store_false") parser.add_argument("--heroku-web-internal", dest="heroku_web_internal", action="store_true", help="This is for internal use only. If you use it, things will go wrong.") arguments = parser.parse_args() logging.debug(arguments) if sys.platform == "win32": # Subprocess support; not needed in 3.8 but not harmful asyncio.set_event_loop(asyncio.ProactorEventLoop()) return arguments
Example #9
Source File: test_keep_alive_timeout.py From sanic with MIT License | 6 votes |
def test_keep_alive_timeout_reuse(): """If the server keep-alive timeout and client keep-alive timeout are both longer than the delay, the client _and_ server will successfully reuse the existing connection.""" try: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) client = ReuseableSanicTestClient(keep_alive_timeout_app_reuse, loop) headers = {"Connection": "keep-alive"} request, response = client.get("/1", headers=headers) assert response.status == 200 assert response.text == "OK" loop.run_until_complete(aio_sleep(1)) request, response = client.get("/1") assert response.status == 200 assert response.text == "OK" finally: client.kill_server()
Example #10
Source File: test_logo.py From sanic with MIT License | 6 votes |
def test_logo_true(app, caplog): app.config.LOGO = True server = app.create_server( debug=True, return_asyncio_server=True, port=PORT ) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop._stopping = False with caplog.at_level(logging.DEBUG): _server = loop.run_until_complete(server) _server.close() loop.run_until_complete(_server.wait_closed()) app.stop() assert caplog.record_tuples[ROW][1] == logging.DEBUG assert caplog.record_tuples[ROW][2] == BASE_LOGO
Example #11
Source File: support.py From molotov with Apache License 2.0 | 6 votes |
def async_test(func): @functools.wraps(func) def _async_test(*args, **kw): cofunc = asyncio.coroutine(func) oldloop = asyncio.get_event_loop() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.set_debug(True) console = SharedConsole(interval=0) results = SharedCounters( "WORKER", "REACHED", "RATIO", "OK", "FAILED", "MINUTE_OK", "MINUTE_FAILED" ) kw["loop"] = loop kw["console"] = console kw["results"] = results try: loop.run_until_complete(cofunc(*args, **kw)) finally: loop.stop() loop.close() asyncio.set_event_loop(oldloop) return _async_test
Example #12
Source File: test_rtm_client_functional.py From python-slackclient with MIT License | 6 votes |
def setUp(self): setup_mock_web_api_server(self) self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) task = asyncio.ensure_future(self.mock_server(), loop=self.loop) self.loop.run_until_complete(asyncio.wait_for(task, 0.1)) self.client = slack.RTMClient( token="xoxb-valid", base_url="http://localhost:8765", auto_reconnect=False, run_async=False, ) self.client._web_client = slack.WebClient( token="xoxb-valid", base_url="http://localhost:8888", run_async=False, )
Example #13
Source File: test_runner.py From resolwe with Apache License 2.0 | 6 votes |
def _run_in_event_loop(coro, *args, **kwargs): """Run a coroutine in a runloop call. This is needed as the top level call into Resolwe Manager-using tests. An event loop is started so that it can be used within the call tree. :param coro: The coroutine to run with an underlying event loop. All other arguments given to this function are forwarded to it. """ asyncio.get_event_loop().close() loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) task = asyncio.ensure_future(coro(*args, **kwargs), loop=loop) loop.run_until_complete(task) loop.close() return task.result()
Example #14
Source File: conftest.py From aioredis with MIT License | 6 votes |
def loop(): """Creates new event loop.""" loop = asyncio.new_event_loop() if sys.version_info < (3, 8): asyncio.set_event_loop(loop) try: yield loop finally: if hasattr(loop, 'is_closed'): closed = loop.is_closed() else: closed = loop._closed # XXX if not closed: loop.call_soon(loop.stop) loop.run_forever() loop.close()
Example #15
Source File: utils.py From bob with GNU General Public License v3.0 | 5 votes |
def __init__(self): import asyncio import multiprocessing import signal import concurrent.futures if sys.platform == 'win32': loop = asyncio.ProactorEventLoop() asyncio.set_event_loop(loop) multiprocessing.set_start_method('spawn') executor = concurrent.futures.ProcessPoolExecutor() else: # The ProcessPoolExecutor is a barely usable for our interactive use # case. On SIGINT any busy executor should stop. The only way how this # does not explode is that we ignore SIGINT before spawning the process # pool and re-enable SIGINT in every executor. In the main process we # have to ignore BrokenProcessPool errors as we will likely hit them. # To "prime" the process pool a dummy workload must be executed because # the processes are spawned lazily. loop = asyncio.get_event_loop() origSigInt = signal.getsignal(signal.SIGINT) signal.signal(signal.SIGINT, signal.SIG_IGN) # fork early before process gets big if sys.platform == 'msys': multiprocessing.set_start_method('fork') else: multiprocessing.set_start_method('forkserver') executor = concurrent.futures.ProcessPoolExecutor() executor.submit(dummy).result() signal.signal(signal.SIGINT, origSigInt) loop.set_default_executor(executor) self.__loop = loop self.__executor = executor
Example #16
Source File: test_ssh.py From heralding with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) self.reporting_relay = ReportingRelay() self.reporting_relay_task = self.loop.run_in_executor( None, self.reporting_relay.start)
Example #17
Source File: asyncio.py From opendevops with GNU General Public License v3.0 | 5 votes |
def get_event_loop(self) -> asyncio.AbstractEventLoop: try: return super().get_event_loop() except (RuntimeError, AssertionError): # This was an AssertionError in python 3.4.2 (which ships with debian jessie) # and changed to a RuntimeError in 3.4.3. # "There is no current event loop in thread %r" loop = self.new_event_loop() self.set_event_loop(loop) return loop
Example #18
Source File: test_vnc.py From heralding with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) self.reporting_relay = ReportingRelay() self.reporting_relay_task = self.loop.run_in_executor( None, self.reporting_relay.start)
Example #19
Source File: asyncio.py From opendevops with GNU General Public License v3.0 | 5 votes |
def _clear_current_hook(self) -> None: if self.is_current: asyncio.set_event_loop(self.old_asyncio) self.is_current = False
Example #20
Source File: testing_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def setUp(self): # This simulates the effect of an asyncio test harness like # pytest-asyncio. self.orig_loop = asyncio.get_event_loop() self.new_loop = asyncio.new_event_loop() asyncio.set_event_loop(self.new_loop) super(GetNewIOLoopTest, self).setUp()
Example #21
Source File: test_socks5.py From heralding with GNU General Public License v3.0 | 5 votes |
def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(None) self.reporting_relay = ReportingRelay() self.reporting_relay_task = self.loop.run_in_executor( None, self.reporting_relay.start)
Example #22
Source File: websocket_client.py From gabriel with Apache License 2.0 | 5 votes |
def launch(self): event_loop = asyncio.get_event_loop() # TODO remove this line once we stop supporting Python 3.5 asyncio.set_event_loop(event_loop) try: self._websocket = event_loop.run_until_complete( websockets.connect(self._uri)) except ConnectionRefusedError: logger.error('Could not connect to server') return consumer_task = asyncio.ensure_future(self._consumer_handler()) tasks = [ asyncio.ensure_future(self._producer_handler( producer_wrapper.producer, producer_wrapper.source_name)) for producer_wrapper in self.producer_wrappers ] tasks.append(consumer_task) _, pending = event_loop.run_until_complete(asyncio.wait( tasks, return_when=asyncio.FIRST_COMPLETED)) for task in pending: task.cancel() logger.info('Disconnected From Server')
Example #23
Source File: monitoring.py From pyquarkchain with MIT License | 5 votes |
def crawl_bfs(ip, p2p_port, jrpc_port): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) cache = loop.run_until_complete(crawl_async(ip, p2p_port, jrpc_port)) res = {} # we can avoid the loop, but it will look crazy for k, v in cache.items(): res["{}:{}".format(k[0], k[1])] = ["{}:{}".format(p[0], p[1]) for p in v] return res
Example #24
Source File: mock_server.py From aiobotocore with Apache License 2.0 | 5 votes |
def _run(self): asyncio.set_event_loop(asyncio.new_event_loop()) app = aiohttp.web.Application() app.router.add_route('*', '/ok', self.ok) app.router.add_route('*', '/{anything:.*}', self.stream_handler) try: aiohttp.web.run_app(app, host=host, port=self._port, handle_signals=False) except BaseException: pytest.fail('unable to start and connect to aiohttp server') raise
Example #25
Source File: server.py From mars with Apache License 2.0 | 5 votes |
def _configure_loop(): try: ioloop.IOLoop.current() except RuntimeError: import asyncio asyncio.set_event_loop(asyncio.new_event_loop()) loop = None try: loop = ioloop.IOLoop.current() except: # noqa: E722 pass if loop is None: raise
Example #26
Source File: with_notebook.py From vpython-jupyter with MIT License | 5 votes |
def start_server(): asyncio.set_event_loop(asyncio.new_event_loop()) application = tornado.web.Application([(r'/ws', WSHandler),]) http_server = tornado.httpserver.HTTPServer(application) http_server.listen(__SOCKET_PORT) Log = logging.getLogger('tornado.access') level = logging.getLevelName('WARN') Log.setLevel(level) tornado.ioloop.IOLoop.instance().start() # Removed check for ipykernel version because the old check # was for 5.0.0 but this works with 4.x too...and 4.x is the first # version of ipykernel
Example #27
Source File: no_notebook.py From vpython-jupyter with MIT License | 5 votes |
def start_websocket_server(): """ Function to get the websocket server going and run the event loop that feeds it. """ # We need a new loop in case some other process has already started the # main loop. In principle we might be able to do a check for a running # loop but this works whether or not a loop is running. __interact_loop = asyncio.new_event_loop() # Need to do two things before starting the server factory: # # 1. Set our loop to be the default event loop on this thread asyncio.set_event_loop(__interact_loop) # 2. Line below is courtesy of # https://github.com/crossbario/autobahn-python/issues/1007#issuecomment-391541322 txaio.config.loop = __interact_loop # Now create the factory, start the server then run the event loop forever. __factory = WebSocketServerFactory(u"ws://localhost:{}/".format(__SOCKET_PORT)) __factory.protocol = WSserver __coro = __interact_loop.create_server(__factory, '0.0.0.0', __SOCKET_PORT) __interact_loop.run_until_complete(__coro) __interact_loop.run_forever() # Put the websocket server in a separate thread running its own event loop. # That works even if some other program (e.g. spyder) already running an # async event loop.
Example #28
Source File: nest_test.py From nest_asyncio with BSD 2-Clause "Simplified" License | 5 votes |
def setUp(self): self.loop = asyncio.new_event_loop() nest_asyncio.apply(self.loop) asyncio.set_event_loop(self.loop) self.loop.set_debug(True) self.loop.set_exception_handler(exception_handler)
Example #29
Source File: utils.py From aiocouchdb with BSD 2-Clause "Simplified" License | 5 votes |
def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) wraps = None if self._test_target != 'mock': wraps = self._request_tracer(aiocouchdb.client.request) self._patch = mock.patch('aiocouchdb.client.request', wraps=wraps) self.request = self._patch.start() self._set_response(self.prepare_response()) self._req_per_task = defaultdict(list) self.loop.run_until_complete(self.setup_env())
Example #30
Source File: test_azure_iothub_cli_extension.py From azure-uamqp-python with MIT License | 5 votes |
def executor(target, consumer_group, enqueued_time, device_id=None, properties=None, timeout=0): coroutines = [] coroutines.append(initiate_event_monitor(target, consumer_group, enqueued_time, device_id, properties, timeout)) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) future = asyncio.gather(*coroutines, loop=loop, return_exceptions=True) result = None try: device_filter_txt = None if device_id: device_filter_txt = ' filtering on device: {},'.format(device_id) def stop_and_suppress_eloop(): try: loop.stop() except Exception: # pylint: disable=broad-except pass print('Starting event monitor,{} use ctrl-c to stop...'.format(device_filter_txt if device_filter_txt else '')) result = loop.run_until_complete(future) except KeyboardInterrupt: print('Stopping event monitor...') remaining_tasks = [t for t in asyncio.Task.all_tasks() if not t.done()] remaining_future = asyncio.gather(*remaining_tasks, loop=loop, return_exceptions=True) try: loop.run_until_complete(asyncio.wait_for(remaining_future, 5)) except concurrent.futures.TimeoutError: print("Timed out before tasks could finish. Shutting down anyway.") print("Finished event monitor shutdown.") finally: if result: error = next(res for res in result if result) if error: logger.error(error) raise RuntimeError(error)