Python asyncio.run boilerplate

Concurrency is built into Python via asyncio. AsyncIO generators are implemented with yield much like synchronous generators. async for 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. AsyncIO subprocess may need specific asyncio loop configuration. The options needed are not the same for every project, depending on the asynchronous functions used.

asyncio.subprocess

The example date_coro.py uses AsyncIO subprocess, which needs ProactorEventLoop on Windows in project __init__.py:

import os
import asyncio

if os.name == "nt":
    asyncio.set_event_loop_policy(asyncio.WindowsProactorEventLoopPolicy())

asyncio.open_connection

For networking apps using asyncio.open_connection the ProactorEventLoop emits numerous warnings with trapped RuntimeError. A workaround is to not use the ProactorEventLoop. Do this by adding to project __init__.py:

import os
import asyncio

if os.name == "nt":
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())