SuperFastPython.
com Cheat Sheet for Python asyncio
Why asyncio? Task Tasks
Asyncio provides coroutine-based concurrency A task schedules a coroutine to run independently. Operations on an awaitable, task, or tasks.
suited to non-blocking socket I/O applications.
Create and schedule task (high-level) Wait for awaitable with a timeout
Coroutine task = asyncio.create_task(coro) try:
await asyncio.wait_for(tk,
Import Create and schedule task (low-level) timeout=1)
import asyncio task = asyncio.ensure_future(coro) except [Link]:
# ...
Define a coroutine Suspend and wait for a task to finish
async def custom_coroutine(): await task Shield a task from cancelation
# ... shielded = [Link](task)
Get the current task
Create coroutine object task = asyncio.current_task() Run blocking function in new thread
coro = custom_coroutine() coro = asyncio.to_thread(myfunc)
Get all running tasks
Run coroutine as entry point tasks = asyncio.all_tasks() Run coroutine in asyncio event loop
[Link](main()) fut = run_coroutine_threadsafe(coro,
Get task result loop)
Suspend and run coroutine from a coroutine value = [Link]()
await custom_coroutine() Run many awaitables as a group
Get task unhandled exception await [Link](c1(), c2())
Sleep a coroutine ex = [Link]()
await [Link](1) Wait for all tasks in a collection
Cancel a task result done,pen = await [Link](tasks)
Async Comprehensions and Loops was_canceled = [Link]()
Wait for all tasks with a timeout in seconds
Check if the task is done (not running) Try:
Asynchronous comprehension done,pen = await
res = [r async for r in async_gen()] if [Link]():
# ... [Link](tasks, timeout=5)
except [Link]:
Await comprehension Check if the task was canceled # ...
res = [r await a in awaitables] if [Link]():
# ... Wait for the first task in a collection
Asynchronous for-loop done,pen = await [Link](tasks,
async for item in async_gen(): Add done callback function return_when=FIRST_COMPLETED)
print(item) task.add_done_callback(handler)
Wait for the first task to fail
Remove done callback function done,pen = await [Link](tasks,
task.remove_done_callback(handler) return_when=FIRST_EXCEPTION)
Set and get task name Get results in task completion order
task.set_name(‘MyTask’) for c in
name = task.get_name() asyncio.as_completed(tasks):
result = await c
Non-blocking IO Subprocesses Semaphores and Events, and Conditions Async Locks
Run command as subprocess Semaphore, set num positions Mutex lock
p = await semaphore = [Link](10) lock = [Link]()
create_subprocess_exec(‘ls’) await [Link]() await [Link]()
# ... # ...
Run shell command as subprocess [Link]() [Link]()
p = await
create_subprocess_shell(‘ls’) Semaphore, context manager Mutex lock, context manager
async with semaphore: async with lock:
Wait for subprocess to finish # ... # ...
await [Link]()
Create event, then set event Queues
Read from subprocess event = [Link]() Via Queue, LifoQueue, PriorityQueue
data = await [Link]() [Link]()
Create queue
Read from subprocess Check if event is set queue = [Link]()
await if event.is_set():
[Link](input=data) # ... Create queue with limited capacity
queue = [Link](100)
Terminate a subprocess Wait for event to be set (blocking)
[Link]() await [Link]() Add item to queue (blocking, if limited)
await [Link](item)
Non-blocking IO Streams Condition variable
condition = [Link]() Retrieve item from queue (blocking)
Open a client tcp connection await [Link]() item = await [Link]()
reader, writer = await # ...
open_connection(google,com’, 80) [Link]() Check if queue is empty
if [Link]():
Start a tcp server Wait on condition to be notified (blocking) # ...
server = await start_server(handle, async with condition:
‘[Link]’, 9876) await [Link]()
Check if queue is full
if [Link]():
Read from socket Wait on condition for expression (blocking) # ...
data = await [Link]() async with condition:
await condition.wait_for(check)
Get current capacity of queue
Write to socket capacity = [Link]()
[Link](data) Notify any single thread waiting on condition
async with condition: Mark unit of work complete
Drain socket until ready [Link](n=1) queue.task_done()
await [Link]()
Notify all threads waiting on condition Wait for all units to be complete
Close socket connection async with condition: await [Link]()
[Link]() condition.notify_all()
await writer.wait_closed()
Async Generators and Iterators
Define asynchronous generator
async def async_generator():
for i in range(10):
await [Link](1)
yield i
Define asynchronous iterator
class AsyncIterator():
def __init__(self):
[Link] = 0
def __aiter__(self):
return self
async def __anext__(self):
if [Link] >= 10:
raise StopAsyncIteration
await [Link](1)
[Link] += 1
return [Link]
Use asynchronous iterator
async for value in AsyncIterator():
# ...
Async Context Managers
Define asynchronous context manager
class AsyncContextManager():
async def __aenter__(self):
await [Link](1)
def __aexit__(self, et, exc,
tb):
await [Link](1)
Use asynchronous context manager
async with CustomClass() as mgmr:
# ...