Python asyncio.new_event_loop() Examples
The following are 30
code examples of asyncio.new_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: 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 #2
Source File: example.py From tattle with Mozilla Public License 2.0 | 6 votes |
def start_node(): config = configure_node() loop = asyncio.new_event_loop() node = tattle.Cluster(config, loop=loop) thread = NodeThread(node, loop=loop) thread.start() other_node = random.choice(list(all_nodes)) if all_nodes else None all_nodes.add(node) all_threads[node] = thread client = NodeClient(port=config.api_port) if other_node: client.join((other_node.local_node_address, other_node.local_node_port)) return client
Example #3
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 #4
Source File: issue_497.py From python-slackclient with MIT License | 6 votes |
def per_request_async(): try: # This is not optimal and the host should have a large number of FD (File Descriptor) loop_for_this_request = asyncio.new_event_loop() async_client = WebClient( token=os.environ["SLACK_BOT_TOKEN"], run_async=True, loop=loop_for_this_request ) future = async_client.chat_postMessage( channel="#random", text="You used the singleton WebClient for posting this message!" ) response = loop_for_this_request.run_until_complete(future) return str(response) except SlackApiError as e: return make_response(str(e), 400)
Example #5
Source File: util.py From molotov with Apache License 2.0 | 6 votes |
def _run_in_fresh_loop(coro, timeout=30): thres = [] thexc = [] def run(): loop = asyncio.new_event_loop() try: task = loop.create_task(coro(loop=loop)) thres.append(loop.run_until_complete(task)) except Exception as e: thexc.append(e) finally: loop.close() th = threading.Thread(target=run) th.start() th.join(timeout=timeout) # re-raise a thread exception if len(thexc) > 0: raise thexc[0] return thres[0]
Example #6
Source File: test_sharedconsole.py From molotov with Apache License 2.0 | 6 votes |
def run_worker(input): if os.getpid() not in _PROC: _PROC.append(os.getpid()) _CONSOLE.print("hello") try: 3 + "" except Exception: _CONSOLE.print_error("meh") with catch_output() as (stdout, stderr): loop = asyncio.new_event_loop() fut = asyncio.ensure_future(_CONSOLE.display(), loop=loop) loop.run_until_complete(fut) loop.close() stdout = stdout.read() assert stdout == "", stdout
Example #7
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 #8
Source File: runlistener.py From resolwe with Apache License 2.0 | 6 votes |
def handle(self, *args, **kwargs): """Run the executor listener. This method never returns.""" listener = ExecutorListener( redis_params=getattr(settings, "FLOW_MANAGER", {}).get( "REDIS_CONNECTION", {} ) ) def _killer(signum, frame): """Kill the listener on receipt of a signal.""" listener.terminate() signal(SIGINT, _killer) signal(SIGTERM, _killer) async def _runner(): """Run the listener instance.""" if kwargs["clear_queue"]: await listener.clear_queue() async with listener: pass loop = asyncio.new_event_loop() loop.run_until_complete(_runner()) loop.close()
Example #9
Source File: test_keep_alive_timeout.py From sanic with MIT License | 6 votes |
def test_keep_alive_client_timeout(): """If the server keep-alive timeout is longer than the client keep-alive timeout, client will try to create a new connection here.""" try: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) client = ReuseableSanicTestClient(keep_alive_app_client_timeout, loop) headers = {"Connection": "keep-alive"} try: request, response = client.get( "/1", headers=headers, request_keepalive=1 ) assert response.status == 200 assert response.text == "OK" loop.run_until_complete(aio_sleep(2)) exception = None request, response = client.get("/1", request_keepalive=1) except ValueError as e: exception = e assert exception is not None assert isinstance(exception, ValueError) assert "got a new connection" in exception.args[0] finally: client.kill_server()
Example #10
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 #11
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 #12
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 #13
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 #14
Source File: test_asyncio_event_loops.py From python-slackclient with MIT License | 6 votes |
def test_the_cost_of_event_loop_creation(self): # create 100 event loops loops = [] try: upper_limit = 0 for i in range(1000): try: loops.append(asyncio.new_event_loop()) except OSError as e: self.logger.info(f"Got an OSError when creating {i} event loops") self.assertEqual(e.errno, 24) self.assertEqual(e.strerror, "Too many open files") upper_limit = i break self.assertTrue(upper_limit > 0) finally: for loop in loops: loop.close()
Example #15
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 #16
Source File: test_cancel_token.py From pyquarkchain with MIT License | 5 votes |
def test_token_chain_event_loop_mismatch(): token = CancelToken("token") token2 = CancelToken("token2", loop=asyncio.new_event_loop()) with pytest.raises(EventLoopMismatch): token.chain(token2)
Example #17
Source File: asyn.py From filesystem_spec with BSD 3-Clause "New" or "Revised" License | 5 votes |
def get_loop(): """Create a running loop in another thread""" loop = asyncio.new_event_loop() t = threading.Thread(target=loop.run_forever) t.daemon = True t.start() return loop # these methods should be implemented as async by any async-able backend
Example #18
Source File: test_transfer.py From resolwe with Apache License 2.0 | 5 votes |
def run_async(coroutine): loop = asyncio.new_event_loop() return loop.run_until_complete(start(coroutine))
Example #19
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 #20
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 #21
Source File: test_issue_378.py From python-slackclient with MIT License | 5 votes |
def setUp(self): self.logger = logging.getLogger(__name__) self.user_token = os.environ[SLACK_SDK_TEST_USER_TOKEN] self.sync_client: WebClient = WebClient(token=self.user_token, run_async=False, loop=asyncio.new_event_loop()) self.async_client: WebClient = WebClient(token=self.user_token, run_async=True)
Example #22
Source File: base_client.py From python-slackclient with MIT License | 5 votes |
def _get_event_loop(self): """Retrieves the event loop or creates a new one.""" try: return asyncio.get_event_loop() except RuntimeError: loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) return loop
Example #23
Source File: test_web_client.py From python-slackclient with MIT License | 5 votes |
def test_pagination_with_iterator_use_sync_aiohttp(self): client: WebClient = WebClient( token=self.bot_token, run_async=False, use_sync_aiohttp=True, loop=asyncio.new_event_loop(), ) fetched_count = 0 # SlackResponse is an iterator that fetches next if next_cursor is not "" for response in client.conversations_list(limit=1, exclude_archived=1, types="public_channel"): fetched_count += len(response["channels"]) if fetched_count > 1: break self.assertGreater(fetched_count, 1)
Example #24
Source File: test_web_client.py From python-slackclient with MIT License | 5 votes |
def setUp(self): if not hasattr(self, "logger"): self.logger = logging.getLogger(__name__) self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] self.async_client: WebClient = WebClient(token=self.bot_token, run_async=True) self.sync_client: WebClient = WebClient( token=self.bot_token, run_async=False, loop=asyncio.new_event_loop(), # TODO: remove this ) self.channel_id = os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]
Example #25
Source File: test_issue_672.py From python-slackclient with MIT License | 5 votes |
def setUp(self): if not hasattr(self, "logger"): self.logger = logging.getLogger(__name__) self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] self.sync_client: WebClient = WebClient( token=self.bot_token, run_async=False, loop=asyncio.new_event_loop()) self.async_client: WebClient = WebClient(token=self.bot_token, run_async=True) self.channel_id = os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]
Example #26
Source File: multiverse.py From Penny-Dreadful-Tools with GNU General Public License v3.0 | 5 votes |
def init() -> None: event_loop = None try: event_loop = asyncio.get_event_loop() except RuntimeError: event_loop = asyncio.new_event_loop() asyncio.set_event_loop(event_loop) return event_loop.run_until_complete(init_async())
Example #27
Source File: test_issue_670.py From python-slackclient with MIT License | 5 votes |
def setUp(self): self.logger = logging.getLogger(__name__) self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] self.sync_client: WebClient = WebClient(token=self.bot_token, run_async=False, loop=asyncio.new_event_loop()) self.async_client: WebClient = WebClient(token=self.bot_token, run_async=True)
Example #28
Source File: helpers.py From python-slackclient with MIT License | 5 votes |
def async_test(coro): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) def wrapper(*args, **kwargs): future = coro(*args, **kwargs) return asyncio.get_event_loop().run_until_complete(future) return wrapper
Example #29
Source File: test_issue_654.py From python-slackclient with MIT License | 5 votes |
def setUp(self): if not hasattr(self, "logger"): self.logger = logging.getLogger(__name__) self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] self.sync_client: WebClient = WebClient(token=self.bot_token, run_async=False, loop=asyncio.new_event_loop()) self.async_client: WebClient = WebClient(token=self.bot_token, run_async=True) self.channel_id = os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]
Example #30
Source File: test_issue_677.py From python-slackclient with MIT License | 5 votes |
def setUp(self): if not hasattr(self, "logger"): self.logger = logging.getLogger(__name__) self.bot_token = os.environ[SLACK_SDK_TEST_BOT_TOKEN] self.sync_client: WebClient = WebClient( token=self.bot_token, run_async=False, loop=asyncio.new_event_loop()) self.async_client: WebClient = WebClient(token=self.bot_token, run_async=True) self.channel_id = os.environ[SLACK_SDK_TEST_WEB_TEST_CHANNEL_ID]