Questions tagged [python-asyncio]

This tag is to be used for the asyncio Python package which provides mechanisms for writing single-threaded concurrent code. The asyncio package provides asynchronous I/O, event loop, coroutines and tasks beginning with Python 3.4.

This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives. Here is a more detailed list of the package contents:

  • a pluggable event loop with various system-specific implementations;
  • transport and protocol abstractions (similar to those in Twisted);
  • concrete support for TCP, UDP, SSL, subprocess pipes, delayed calls, and others (some may be system-dependent);
  • a Future class that mimicks the one in the concurrent.futures module, but adapted for use with the event loop;
  • coroutines and tasks based on yield from (PEP 380), to help write concurrent code in a sequential fashion;
  • cancellation support for Futures and coroutines;
  • synchronization primitives for use between coroutines in a single thread, mimicking those in the threading module;
  • an interface for passing work off to a threadpool, for times when you absolutely, positively have to use a library that makes blocking I/O calls.
  • Recently Python 3.5 introduced async/await for enhancing the module for more readablity instead of yield from.
7214 questions
79
votes
2 answers

Why do most asyncio examples use loop.run_until_complete()?

I was going through the Python documentation for asyncio and I'm wondering why most examples use loop.run_until_complete() as opposed to Asyncio.ensure_future(). For example: https://docs.python.org/dev/library/asyncio-task.html It seems…
SamuelN
  • 1,341
  • 1
  • 10
  • 19
79
votes
9 answers

When using asyncio, how do you allow all running tasks to finish before shutting down the event loop

I have the following code: @asyncio.coroutine def do_something_periodically(): while True: asyncio.async(my_expensive_operation()) yield from asyncio.sleep(my_interval) if shutdown_flag_is_set: print("Shutting…
derekdreery
  • 3,860
  • 4
  • 29
  • 38
78
votes
2 answers

How to call a async function contained in a class?

Based on this answer I want to build an async websoket client in a class which would be imported from another file: #!/usr/bin/env python3 import sys, json import asyncio from websockets import connect class EchoWebsocket: def…
mllamazares
  • 7,876
  • 17
  • 61
  • 89
76
votes
5 answers

how to add a coroutine to a running asyncio loop?

How can one add a new coroutine to a running asyncio loop? Ie. one that is already executing a set of coroutines. I guess as a workaround one could wait for existing coroutines to complete and then initialize a new loop (with the additional…
Petri
  • 4,796
  • 2
  • 22
  • 31
76
votes
3 answers

How to combine python asyncio with threads?

I have successfully built a RESTful microservice with Python asyncio and aiohttp that listens to a POST event to collect realtime events from various feeders. It then builds an in-memory structure to cache the last 24h of events in a nested…
fxstein
  • 1,133
  • 1
  • 9
  • 12
75
votes
4 answers

python-asyncio TypeError: object dict can't be used in 'await' expression

I am using a third party module to retrieve data from an API. I simply would like to asynchronously await the module to return the data which occasionally takes several seconds and freezes up my app. However, when I try to await a call to that…
Riley Hughes
  • 1,344
  • 2
  • 12
  • 22
74
votes
5 answers

How do I run Python asyncio code in a Jupyter notebook?

I have some asyncio code which runs fine in the Python interpreter (CPython 3.6.2). I would now like to run this inside a Jupyter notebook with an IPython kernel. I can run it with import asyncio asyncio.get_event_loop().run_forever() and while…
snth
  • 5,194
  • 4
  • 39
  • 48
74
votes
9 answers

Mocking async call in python 3.5

How do I mock async call from one native coroutine to other one using unittest.mock.patch? I currently have quite an awkward solution: class CoroutineMock(MagicMock): def __await__(self, *args, **kwargs): future = Future() …
Zozz
  • 1,875
  • 1
  • 14
  • 14
69
votes
10 answers

How to combine Celery with asyncio?

How can I create a wrapper that makes celery tasks look like asyncio.Task? Or is there a better way to integrate Celery with asyncio? @asksol, the creator of Celery, said this:: It's quite common to use Celery as a distributed layer on top of async…
max
  • 49,282
  • 56
  • 208
  • 355
66
votes
5 answers

Make a Python asyncio call from a Flask route

I want to execute an async function every time the Flask route is executed. Why is the abar function never executed? import asyncio from flask import Flask async def abar(a): print(a) loop = asyncio.get_event_loop() app =…
user24502
  • 1,662
  • 4
  • 16
  • 21
66
votes
5 answers

What's the correct way to clean up after an interrupted event loop?

I have an event loop that runs some co-routines as part of a command line tool. The user may interrupt the tool with the usual Ctrl + C, at which point I want to clean up properly after the interrupted event loop. Here's what I tried. import…
Nick Chammas
  • 11,843
  • 8
  • 56
  • 115
65
votes
1 answer

Asyncio vs. Gevent

Background I once worked on a Python2 system that had a lot of custom I/O code written synchronously, and was scaled using threads. At some point, we couldn't scale it any further, and realised we have to switch to asynchronous programming. Twisted…
Dan Gittik
  • 3,460
  • 3
  • 17
  • 24
64
votes
4 answers

How to use asyncio with existing blocking library?

I have a few blocking functions foo, bar and I can't change those (Some internal library I don't control. Talks to one or more network services). How do I use it as async? E.g. I want to do the following: results = [] for inp in inps: val =…
balki
  • 26,394
  • 30
  • 105
  • 151
64
votes
5 answers

What does "SSLError: [SSL] PEM lib (_ssl.c:2532)" mean using the Python ssl library?

I am trying to use connect to another party using Python 3 asyncio module and get this error: 36 sslcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1) ---> 37 sslcontext.load_cert_chain(cert, keyfile=ca_cert) 38 SSLError: [SSL] PEM lib…
sargas
  • 5,820
  • 7
  • 50
  • 69
63
votes
5 answers

How can I call an async function without await?

I have a controller action in aiohttp application. async def handler_message(request): try: content = await request.json() perform_message(x,y,z) except (RuntimeError): print("error in perform fb message") …
Pasalino
  • 992
  • 1
  • 9
  • 15