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
113
votes
3 answers

"Asyncio Event Loop is Closed" when getting loop

When trying to run the asyncio hello world code example given in the docs: import asyncio async def hello_world(): print("Hello World!") loop = asyncio.get_event_loop() # Blocking call which returns when the hello_world() coroutine is…
TryingToTry
  • 1,033
  • 2
  • 9
  • 9
110
votes
2 answers

asyncio.sleep() vs time.sleep()

When I go to the asyncio page, the first example is a hello world program. When I run it on python 3.73, I can't see any different from the normal one. can anyone tell me the difference and give a non-trivial example? In [1]: import asyncio ...: …
an offer can't refuse
  • 4,245
  • 5
  • 30
  • 50
107
votes
9 answers

How can I periodically execute a function with asyncio?

I'm migrating from tornado to asyncio, and I can't find the asyncio equivalent of tornado's PeriodicCallback. (A PeriodicCallback takes two arguments: the function to run and the number of milliseconds between calls.) Is there such an equivalent in…
2Cubed
  • 3,401
  • 7
  • 23
  • 40
103
votes
3 answers

Learning asyncio: "coroutine was never awaited" warning error

I am trying to learn to use asyncio in Python to optimize scripts. My example returns a coroutine was never awaited warning, can you help to understand and find how to solve it? import time import datetime import random import asyncio import…
Anthony Hervy
  • 1,201
  • 2
  • 7
  • 14
100
votes
1 answer

What does asyncio.create_task() do?

What does asyncio.create_task() do? I have looked at the docs and can't seem to understand it. A bit of code that confuses me is this: import asyncio async def counter_loop(x, n): for i in range(1, n + 1): print(f"Counter {x}: {i}") …
BeastCoder
  • 2,391
  • 3
  • 15
  • 26
100
votes
5 answers

Combine awaitables like Promise.all

In asynchronous JavaScript, it is easy to run tasks in parallel and wait for all of them to complete using Promise.all: async function bar(i) { console.log('started', i); await delay(1000); console.log('finished', i); } async function foo()…
Tamas Hegedus
  • 28,755
  • 12
  • 63
  • 97
95
votes
4 answers

Python3.6 AttributeError: module 'asyncio' has no attribute 'run'

I tried to read https://hackernoon.com/asynchronous-python-45df84b82434. It's about asynchronous python and I tried the code from this, but I'm getting a weird Error. The code is: ` import asyncio import aiohttp urls = ['http://www.google.com',…
Jirka Svítil
  • 1,134
  • 1
  • 7
  • 10
94
votes
3 answers

How to properly create and run concurrent tasks using python's asyncio module?

I am trying to properly understand and implement two concurrently running Task objects using Python 3's relatively new asyncio module. In a nutshell, asyncio seems designed to handle asynchronous processes and concurrent Task execution over an event…
songololo
  • 4,724
  • 5
  • 35
  • 49
92
votes
6 answers

How can I wrap a synchronous function in an async coroutine?

I'm using aiohttp to build an API server that sends TCP requests off to a seperate server. The module that sends the TCP requests is synchronous and a black box for my purposes. So my problem is that these requests are blocking the entire API. I…
Zac Delventhal
  • 3,543
  • 3
  • 20
  • 26
85
votes
1 answer

Using asyncio.Queue for producer-consumer flow

I'm confused about how to use asyncio.Queue for a particular producer-consumer pattern in which both the producer and consumer operate concurrently and independently. First, consider this example, which closely follows that from the docs for…
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
85
votes
4 answers

What kind of problems (if any) would there be combining asyncio with multiprocessing?

As almost everyone is aware when they first look at threading in Python, there is the GIL that makes life miserable for people who actually want to do processing in parallel - or at least give it a chance. I am currently looking at implementing…
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
82
votes
4 answers

Getting values from functions that run as asyncio tasks

I was trying the following code: import asyncio @asyncio.coroutine def func_normal(): print("A") yield from asyncio.sleep(5) print("B") return 'saad' @asyncio.coroutine def func_infinite(): i = 0 while…
Saad Aleem
  • 1,709
  • 1
  • 12
  • 18
80
votes
4 answers

How to use `async for` in Python?

I mean what do I get from using async for. Here is the code I write with async for, AIter(10) could be replaced with get_range(). But the code runs like sync not async. import asyncio async def get_range(): for i in range(10): …
PaleNeutron
  • 2,543
  • 4
  • 25
  • 43
80
votes
3 answers

How to use 'yield' inside async function?

I want to use generator yield and async functions. I read this topic, and wrote next code: import asyncio async def createGenerator(): mylist = range(3) for i in mylist: await asyncio.sleep(1) yield i*i async def start(): …
Ильдар
  • 963
  • 1
  • 7
  • 11
80
votes
2 answers

Asynchronous exception handling in Python

I've the following code using asyncio and aiohttp to make asynchronous HTTP requests. import sys import asyncio import aiohttp @asyncio.coroutine def get(url): try: print('GET %s' % url) resp = yield from aiohttp.request('GET',…
Yury Bayda
  • 1,023
  • 1
  • 9
  • 20