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
62
votes
2 answers

what's Python asyncio.Lock() for?

Is it because coroutines may be preempted in the future? Or it allows people to use yield from in critical section (which IMO shouldn't be encouraged)?
user3761759
  • 715
  • 1
  • 5
  • 8
61
votes
1 answer

What does the "yield from" syntax do in asyncio and how is it different from "await"

From the perspective of someone who has written asyncio code but is looking to better understand the inner workings, what is yield from, await and how are those useful for allowing asynchronous code? There is one highly upvoted question asking about…
Azsgy
  • 3,139
  • 2
  • 29
  • 40
61
votes
4 answers

Please explain "Task was destroyed but it is pending!" after cancelling tasks

I am learning asyncio with Python 3.4.2 and I use it to continuously listen on an IPC bus, while gbulb listens on the DBus. I created a function listen_to_ipc_channel_layer that continuously listens for incoming messages on the IPC channel and…
Daniel
  • 3,092
  • 3
  • 32
  • 49
58
votes
2 answers

Using queues results in asyncio exception "got Future attached to a different loop"

I'm trying to run this simple code with asyncio queues, but catch exceptions, and even nested exceptions. I would like to get some help with making queues in asyncio work correctly: import asyncio,…
Shirkan
  • 859
  • 1
  • 9
  • 14
58
votes
4 answers

Does asyncio supports asynchronous I/O for file operations?

Does asyncio supports asynchronous I/O for file operations? If yes, how I can use this in Python 3.5 with async/await syntax code?
CthUlhUzzz
  • 637
  • 1
  • 5
  • 4
58
votes
2 answers

What is the difference between concurrent.futures and asyncio.futures?

To clarify the reason for this question: It is confusing to use two modules with the same name. What do they represent that makes them distinct? What task(s) can one solve that the other can't and vice-versa?
sargas
  • 5,820
  • 7
  • 50
  • 69
57
votes
2 answers

When to use and when not to use Python 3.5 `await` ?

I'm getting the flow of using asyncio in Python 3.5 but I haven't seen a description of what things I should be awaiting and things I should not be or where it would be neglible. Do I just have to use my best judgement in terms of "this is an IO…
dalanmiller
  • 3,467
  • 5
  • 31
  • 38
56
votes
2 answers

Python simple socket client/server using asyncio

I would like to re-implement my code using asyncio coroutines instead of multi-threading. server.py def handle_client(client): request = None while request != 'quit': request = client.recv(255).decode('utf8') response =…
srjjio
  • 930
  • 1
  • 8
  • 16
54
votes
2 answers

How can I send an HTTP request from my FastAPI app to another site (API)?

I am trying to send 100 requests at a time to a server http://httpbin.org/uuid using the following code snippet from fastapi import FastAPI from time import sleep from time import time import requests import asyncio app = FastAPI() URL=…
john mich
  • 2,477
  • 3
  • 17
  • 32
54
votes
2 answers

Python: what are the advantages of async over threads?

I've had a hard time trying to understand how and why async functionality works in python and I am still not sure I understand everything correctly (especially the 'why' part). Please correct me if I am wrong. The purpose of both async methods and…
lesnik
  • 2,507
  • 2
  • 25
  • 24
54
votes
3 answers

@asyncio.coroutine vs async def

With the asyncio library I've seen, @asyncio.coroutine def function(): ... and async def function(): ... used interchangeably. Is there any functional difference between the two?
Jason
  • 2,278
  • 2
  • 17
  • 25
54
votes
6 answers

Asyncio two loops for different I/O tasks?

I am using Python3 Asyncio module to create a load balancing application. I have two heavy IO tasks: A SNMP polling module, which determines the best possible server A "proxy-like" module, which balances the petitions to the selected server. Both…
brunoop
  • 949
  • 1
  • 7
  • 10
50
votes
2 answers

Duplication of code for synchronous and asynchronous implementations

When implementing classes that have uses in both synchronous and asynchronous applications, I find myself maintaining virtually identical code for both use cases. Just as an example, consider: from time import sleep import asyncio class…
Grismar
  • 27,561
  • 4
  • 31
  • 54
49
votes
4 answers

How to use django 3.0 ORM in a Jupyter Notebook without triggering the async context check?

Django 3.0 is adding asgi / async support and with it a guard around making synchronous requests in an async context. Concurrently, IPython just added top level async/await support, which seems to be running the whole interpreter session inside of a…
michalwols
  • 648
  • 1
  • 5
  • 8
49
votes
4 answers

aiohttp: set maximum number of requests per second

How can I set maximum number of requests per second (limit them) in client side using aiohttp?
v18o
  • 1,237
  • 2
  • 15
  • 25