Python asyncio.set_event_loop_policy() Examples
The following are 30
code examples of asyncio.set_event_loop_policy().
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: conftest.py From DjangoChannelsGraphqlWs with MIT License | 7 votes |
def event_loop(request): """Overwrite `pytest_asyncio` eventloop to fix Windows issue. Default implementation causes `NotImplementedError` on Windows with Python 3.8, because they changed default eventloop in 3.8. NOTE: We do the same thing in the `example/settings.py` because it imports (and fails) before we have a chance to invoke this fixture. So, we could avoid adding this fixture, but I feel it is better to keep the proper solution here as well. """ if sys.platform == "win32" and sys.version_info.minor >= 8: asyncio.set_event_loop_policy( asyncio.WindowsSelectorEventLoopPolicy() # pylint: disable=no-member ) loop = asyncio.get_event_loop_policy().new_event_loop() yield loop loop.close()
Example #2
Source File: run.py From hypercorn with MIT License | 6 votes |
def uvloop_worker( config: Config, sockets: Optional[Sockets] = None, shutdown_event: Optional[EventType] = None ) -> None: try: import uvloop except ImportError as error: raise Exception("uvloop is not installed") from error else: asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) app = load_application(config.application_path) shutdown_trigger = None if shutdown_event is not None: shutdown_trigger = partial(check_multiprocess_shutdown_event, shutdown_event, asyncio.sleep) _run( partial(worker_serve, app, config, sockets=sockets), debug=config.debug, shutdown_trigger=shutdown_trigger, )
Example #3
Source File: test_events.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
def test_get_event_loop_returns_running_loop(self): class Policy(asyncio.DefaultEventLoopPolicy): def get_event_loop(self): raise NotImplementedError loop = None old_policy = asyncio.get_event_loop_policy() try: asyncio.set_event_loop_policy(Policy()) loop = asyncio.new_event_loop() self.assertIs(asyncio._get_running_loop(), None) async def func(): self.assertIs(asyncio.get_event_loop(), loop) self.assertIs(asyncio._get_running_loop(), loop) loop.run_until_complete(func()) finally: asyncio.set_event_loop_policy(old_policy) if loop is not None: loop.close() self.assertIs(asyncio._get_running_loop(), None)
Example #4
Source File: guy.py From guy with Apache License 2.0 | 6 votes |
def __init__(self,instance,host="localhost",port=None,autoreload=False): super(WebServer, self).__init__() self.app=None self.instance=instance self.host=host self.autoreload=autoreload if port is not None: self.port = port while not isFree("localhost", self.port): self.port += 1 self.instance._webserver=(self.host,self.port) try: # https://bugs.python.org/issue37373 FIX: tornado/py3.8 on windows if sys.platform == 'win32': asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) except: pass
Example #5
Source File: cli.py From aiodnsbrute with GNU General Public License v3.0 | 6 votes |
def __init__(self, verbosity=0, max_tasks=512): """Constructor. Args: verbosity: set output verbosity: 0 (default) is none, 3 is debug max_tasks: the maximum number of tasks asyncio will queue (default 512) """ self.tasks = [] self.errors = [] self.fqdn = [] self.ignore_hosts = [] asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) self.loop = asyncio.get_event_loop() self.resolver = aiodns.DNSResolver(loop=self.loop, rotate=True) self.sem = asyncio.BoundedSemaphore(max_tasks) self.max_tasks = max_tasks self.verbosity = verbosity self.logger = ConsoleLogger(verbosity)
Example #6
Source File: simple_produce_bench.py From aiokafka with Apache License 2.0 | 6 votes |
def main(): args = parse_args() if args.uvloop: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() task = loop.create_task(Benchmark(args).bench_simple()) task.add_done_callback(lambda _, loop=loop: loop.stop()) def signal_hndl(_task=task): _task.cancel() loop.add_signal_handler(signal.SIGTERM, signal_hndl) loop.add_signal_handler(signal.SIGINT, signal_hndl) try: loop.run_forever() finally: loop.close() if not task.cancelled(): task.result()
Example #7
Source File: simple_consume_bench.py From aiokafka with Apache License 2.0 | 6 votes |
def main(): args = parse_args() if args.uvloop: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() task = loop.create_task(Benchmark(args).bench_simple()) task.add_done_callback(lambda _, loop=loop: loop.stop()) def signal_hndl(_task=task): _task.cancel() loop.add_signal_handler(signal.SIGTERM, signal_hndl) loop.add_signal_handler(signal.SIGINT, signal_hndl) try: loop.run_forever() finally: loop.close() if not task.cancelled(): task.result()
Example #8
Source File: app.py From TabPy with MIT License | 6 votes |
def _init_asyncio_patch(): """ Select compatible event loop for Tornado 5+. As of Python 3.8, the default event loop on Windows is `proactor`, however Tornado requires the old default "selector" event loop. As Tornado has decided to leave this to users to set, MkDocs needs to set it. See https://github.com/tornadoweb/tornado/issues/2608. """ if sys.platform.startswith("win") and sys.version_info >= (3, 8): import asyncio try: from asyncio import WindowsSelectorEventLoopPolicy except ImportError: pass # Can't assign a policy which doesn't exist. else: if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy): asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
Example #9
Source File: webapp.py From teleport with Apache License 2.0 | 6 votes |
def init(self, path_app_root, path_data): log.initialize() asyncio.set_event_loop_policy(tornado.platform.asyncio.AnyThreadEventLoopPolicy()) cfg = tp_cfg() cfg.app_path = path_app_root cfg.static_path = os.path.join(path_app_root, 'static') cfg.template_path = os.path.join(path_app_root, 'view') cfg.res_path = os.path.join(path_app_root, 'res') cfg.data_path = path_data cfg.cfg_path = os.path.join(path_data, 'etc') cfg.log_path = os.path.join(path_data, 'log') self._cfg_file = os.path.join(cfg.cfg_path, 'web.ini') if not cfg.load(self._cfg_file): return False return True
Example #10
Source File: __main__.py From pydis with ISC License | 6 votes |
def main() -> int: print("Hello, World!") asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() # Each client connection will create a new protocol instance coro = loop.create_server(RedisProtocol, "127.0.0.1", 7878) server = loop.run_until_complete(coro) # Serve requests until Ctrl+C is pressed print('Serving on {}'.format(server.sockets[0].getsockname())) try: loop.run_forever() except KeyboardInterrupt: pass # Close the server server.close() loop.run_until_complete(server.wait_closed()) loop.close() return 0
Example #11
Source File: run.py From hypercorn with MIT License | 6 votes |
def asyncio_worker( config: Config, sockets: Optional[Sockets] = None, shutdown_event: Optional[EventType] = None ) -> None: app = load_application(config.application_path) shutdown_trigger = None if shutdown_event is not None: shutdown_trigger = partial(check_multiprocess_shutdown_event, shutdown_event, asyncio.sleep) if config.workers > 1 and platform.system() == "Windows": asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) # type: ignore _run( partial(worker_serve, app, config, sockets=sockets), debug=config.debug, shutdown_trigger=shutdown_trigger, )
Example #12
Source File: test_uvloop_integration.py From aiologger with MIT License | 6 votes |
def test_it_logs_messages(self): asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() async def test(): reader = asyncio.StreamReader(loop=loop) protocol = asyncio.StreamReaderProtocol(reader) transport, _ = await loop.connect_read_pipe( lambda: protocol, self.read_pipe ) logger = Logger.with_default_handlers() await logger.info("Xablau") logged_content = await reader.readline() self.assertEqual(logged_content, b"Xablau\n") transport.close() await logger.shutdown() loop.run_until_complete(test())
Example #13
Source File: simple_produce_bench.py From aiokafka with Apache License 2.0 | 6 votes |
def main(): args = parse_args() if args.uvloop: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() task = loop.create_task(Benchmark(args).bench_simple()) task.add_done_callback(lambda _, loop=loop: loop.stop()) def signal_hndl(_task=task): _task.cancel() loop.add_signal_handler(signal.SIGTERM, signal_hndl) loop.add_signal_handler(signal.SIGINT, signal_hndl) try: loop.run_forever() finally: loop.close() if not task.cancelled(): task.result()
Example #14
Source File: simple_consume_bench.py From aiokafka with Apache License 2.0 | 6 votes |
def main(): args = parse_args() if args.uvloop: import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() task = loop.create_task(Benchmark(args).bench_simple()) task.add_done_callback(lambda _, loop=loop: loop.stop()) def signal_hndl(_task=task): _task.cancel() loop.add_signal_handler(signal.SIGTERM, signal_hndl) loop.add_signal_handler(signal.SIGINT, signal_hndl) try: loop.run_forever() finally: loop.close() if not task.cancelled(): task.result()
Example #15
Source File: serve.py From mkdocs with BSD 2-Clause "Simplified" License | 6 votes |
def _init_asyncio_patch(): """ Select compatible event loop for Tornado 5+. As of Python 3.8, the default event loop on Windows is `proactor`, however Tornado requires the old default "selector" event loop. As Tornado has decided to leave this to users to set, MkDocs needs to set it. See https://github.com/tornadoweb/tornado/issues/2608. """ if sys.platform.startswith("win") and sys.version_info >= (3, 8): import asyncio try: from asyncio import WindowsSelectorEventLoopPolicy except ImportError: pass # Can't assign a policy which doesn't exist. else: if not isinstance(asyncio.get_event_loop_policy(), WindowsSelectorEventLoopPolicy): asyncio.set_event_loop_policy(WindowsSelectorEventLoopPolicy())
Example #16
Source File: worker.py From lambda-text-extractor with Apache License 2.0 | 5 votes |
def init_process(self): import uvloop # Close any existing event loop before setting a # new policy. asyncio.get_event_loop().close() # Setup uvloop policy, so that every # asyncio.get_event_loop() will create an instance # of uvloop event loop. asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) super().init_process()
Example #17
Source File: launch.py From pyrobud with MIT License | 5 votes |
def setup_asyncio(config: util.config.Config) -> asyncio.AbstractEventLoop: """Returns a new asyncio event loop with settings from the given config.""" asyncio_config: util.config.AsyncIOConfig = config["asyncio"] if sys.platform == "win32": # Force ProactorEventLoop on Windows for subprocess support policy = asyncio.WindowsProactorEventLoopPolicy() asyncio.set_event_loop_policy(policy) elif not asyncio_config["disable_uvloop"]: # Initialize uvloop if available try: # noinspection PyUnresolvedReferences import uvloop uvloop.install() log.info("Using uvloop event loop") except ImportError: pass loop = asyncio.new_event_loop() if asyncio_config["debug"]: log.info("Enabling asyncio debug mode") loop.set_debug(True) return loop
Example #18
Source File: worker.py From lambda-text-extractor with Apache License 2.0 | 5 votes |
def init_process(self): import tokio # Close any existing event loop before setting a # new policy. asyncio.get_event_loop().close() # Setup tokio policy, so that every # asyncio.get_event_loop() will create an instance # of tokio event loop. asyncio.set_event_loop_policy(tokio.EventLoopPolicy()) super().init_process()
Example #19
Source File: worker.py From lambda-text-extractor with Apache License 2.0 | 5 votes |
def init_process(self): import uvloop # Close any existing event loop before setting a # new policy. asyncio.get_event_loop().close() # Setup uvloop policy, so that every # asyncio.get_event_loop() will create an instance # of uvloop event loop. asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) super().init_process()
Example #20
Source File: conftest.py From jikanpy with MIT License | 5 votes |
def pytest_sessionstart(session): """ Called after the Session object has been created and before performing collection and entering the run test loop. """ # The default event loop on Windows causes an exception saying Event loop # closed to be thrown on Python 3.8 with Windows # https://github.com/encode/httpx/issues/914 if ( sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith("win") ): asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
Example #21
Source File: test_events.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_set_event_loop_policy(self): self.assertRaises( AssertionError, asyncio.set_event_loop_policy, object()) old_policy = asyncio.get_event_loop_policy() policy = asyncio.DefaultEventLoopPolicy() asyncio.set_event_loop_policy(policy) self.assertIs(policy, asyncio.get_event_loop_policy()) self.assertIsNot(policy, old_policy)
Example #22
Source File: conftest.py From aiotask-context with MIT License | 5 votes |
def uvloop_loop(): asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) loop = asyncio.get_event_loop() yield loop loop.close() # restore the virgin state asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
Example #23
Source File: conftest.py From aiotask-context with MIT License | 5 votes |
def asyncio_loop(): asyncio.set_event_loop_policy(None) loop = asyncio.get_event_loop() yield loop loop.close() # restore the virgin state asyncio.set_event_loop_policy(None)
Example #24
Source File: test_events.py From annotated-py-projects with MIT License | 5 votes |
def test_set_event_loop_policy(self): self.assertRaises( AssertionError, asyncio.set_event_loop_policy, object()) old_policy = asyncio.get_event_loop_policy() policy = asyncio.DefaultEventLoopPolicy() asyncio.set_event_loop_policy(policy) self.assertIs(policy, asyncio.get_event_loop_policy()) self.assertIsNot(policy, old_policy)
Example #25
Source File: claris_randomizer.py From randovania with GNU General Public License v3.0 | 5 votes |
def _process_command(args: List[str], input_data: str, read_callback: Callable[[str], None]): work = _process_command_async(args, input_data, read_callback) if IO_LOOP is None: if _is_windows(): asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy()) asyncio.run(work) else: asyncio.run_coroutine_threadsafe(work, IO_LOOP).result()
Example #26
Source File: case.py From asynctest with Apache License 2.0 | 5 votes |
def _unset_loop(self): policy = asyncio.get_event_loop_policy() if not self.use_default_loop: if sys.version_info >= (3, 6): self.loop.run_until_complete(self.loop.shutdown_asyncgens()) self.loop.close() policy.reset_watcher() asyncio.set_event_loop_policy(policy.original_policy) self.loop = None
Example #27
Source File: case.py From asynctest with Apache License 2.0 | 5 votes |
def _init_loop(self): if self.use_default_loop: self.loop = asyncio.get_event_loop() loop = None else: loop = self.loop = asyncio.new_event_loop() policy = _Policy(asyncio.get_event_loop_policy(), loop, self.forbid_get_event_loop) asyncio.set_event_loop_policy(policy) self.loop = self._patch_loop(self.loop)
Example #28
Source File: utils.py From postfix-mta-sts-resolver with MIT License | 5 votes |
def enable_uvloop(): # pragma: no cover try: # pylint: disable=import-outside-toplevel import uvloop asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) except ImportError: return False else: return True
Example #29
Source File: testasync.py From mock with BSD 2-Clause "Simplified" License | 5 votes |
def tearDownModule(): asyncio.set_event_loop_policy(None)
Example #30
Source File: pytest_plugin.py From Galaxy_Plugin_Bethesda with MIT License | 5 votes |
def loop(loop_factory, fast, loop_debug): # type: ignore """Return an instance of the event loop.""" policy = loop_factory() asyncio.set_event_loop_policy(policy) with loop_context(fast=fast) as _loop: if loop_debug: _loop.set_debug(True) # pragma: no cover asyncio.set_event_loop(_loop) yield _loop