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 asyncio.TimeoutError:
# ...
Define a coroutine Suspend and wait for a task to finish
async def custom_coroutine(): await task Shield a task from cancelation
# ... shielded = asyncio.shield(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
asyncio.run(main()) fut = run_coroutine_threadsafe(coro,
Get task result loop)
Suspend and run coroutine from a coroutine value = task.result()
await custom_coroutine() Run many awaitables as a group
Get task unhandled exception await asyncio.gather(c1(), c2())
Sleep a coroutine ex = task.exception()
await asyncio.sleep(1) Wait for all tasks in a collection
Cancel a task result done,pen = await asyncio.wait(tasks)
Async Comprehensions and Loops was_canceled = task.cancel()
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 task.done():
# ... asyncio.wait(tasks, timeout=5)
except asyncio.TimeoutError:
Await comprehension Check if the task was canceled # ...
res = [r await a in awaitables] if task.cancelled():
# ... Wait for the first task in a collection
Asynchronous for-loop done,pen = await asyncio.wait(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 asyncio.wait(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 = asyncio.Semaphore(10) lock = asyncio.Lock()
create_subprocess_exec(‘ls’) await semaphore.acquire() await lock.acquire()
# ... # ...
Run shell command as subprocess semaphore.release() lock.release()
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 process.wait()
Create event, then set event Queues
Read from subprocess event = asyncio.Event() Via Queue, LifoQueue, PriorityQueue
data = await process.communicate() event.set()
Create queue
Read from subprocess Check if event is set queue = asyncio.Queue()
await if event.is_set():
process.communicate(input=data) # ... Create queue with limited capacity
queue = asyncio.Queue(100)
Terminate a subprocess Wait for event to be set (blocking)
process.terminate() await event.wait() Add item to queue (blocking, if limited)
await queue.put(item)
Non-blocking IO Streams Condition variable
condition = asyncio.Condition() Retrieve item from queue (blocking)
Open a client tcp connection await condition.acquire() item = await queue.get()
reader, writer = await # ...
open_connection(google,com’, 80) condition.release() Check if queue is empty
if queue.empty():
Start a tcp server Wait on condition to be notified (blocking) # ...
server = await start_server(handle, async with condition:
‘127.0.0.1’, 9876) await condition.wait()
Check if queue is full
if queue.full():
Read from socket Wait on condition for expression (blocking) # ...
data = await reader.readline() async with condition:
await condition.wait_for(check)
Get current capacity of queue
Write to socket capacity = queue.qsize()
writer.write(data) Notify any single thread waiting on condition
async with condition: Mark unit of work complete
Drain socket until ready condition.notify(n=1) queue.task_done()
await writer.drain()
Notify all threads waiting on condition Wait for all units to be complete
Close socket connection async with condition: await queue.join()
writer.close() condition.notify_all()
await writer.wait_closed()
Async Generators and Iterators
Define asynchronous generator
async def async_generator():
for i in range(10):
await asyncio.sleep(1)
yield i
Define asynchronous iterator
class AsyncIterator():
def __init__(self):
self.counter = 0
def __aiter__(self):
return self
async def __anext__(self):
if self.counter >= 10:
raise StopAsyncIteration
await asyncio.sleep(1)
self.counter += 1
return self.counter
Use asynchronous iterator
async for value in AsyncIterator():
# ...
Async Context Managers
Define asynchronous context manager
class AsyncContextManager():
async def __aenter__(self):
await asyncio.sleep(1)
def __aexit__(self, et, exc,
tb):
await asyncio.sleep(1)
Use asynchronous context manager
async with CustomClass() as mgmr:
# ...