Python asyncio.run boilerplate
Concurrency is built into Python via
AsyncIO generators are implemented with
yield much like synchronous generators.
also simplifies expression of asynchronous for loops.
As in Julia, the expression of asynchronous structures in Python does not implement concurrent execution. Concurrent execution in Python is governed by collections of tasks or futures such as asyncio.gather and initiated by a runner such as asyncio.run
asyncio.run() doesn’t handle all use cases.
may need specific
asyncio loop configuration.
The options needed are not the same for every project, depending on the asynchronous functions used.
uses AsyncIO subprocess, which needs ProactorEventLoop on Windows in project
import os import asyncio if os.name == "nt": asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())
For networking apps using
the ProactorEventLoop emits numerous warnings with trapped RuntimeError.
A workaround is to not use the ProactorEventLoop.
Do this by adding to project
import os import asyncio if os.name == "nt": asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())