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.
4746 questions
190
votes
5 answers

Asyncio.gather vs asyncio.wait

asyncio.gather and asyncio.wait seem to have similar uses: I have a bunch of async things that I want to execute/wait for (not necessarily waiting for one to finish before the next one starts). They use a different syntax, and differ in some…
Claude
  • 6,353
  • 3
  • 31
  • 48
176
votes
4 answers

How does asyncio actually work?

This question is motivated by my another question: How to await in cdef? There are tons of articles and blog posts on the web about asyncio, but they are all very superficial. I couldn't find any information about how asyncio is actually…
wvxvw
  • 4,882
  • 7
  • 22
  • 44
150
votes
4 answers

multiprocessing vs multithreading vs asyncio in Python 3

I found that in Python 3.4 there are few different libraries for multiprocessing/threading: multiprocessing vs threading vs asyncio. But I don't know which one to use or is the "recommended one". Do they do the same thing, or are different? If so,…
142
votes
4 answers

"Fire and forget" python async/await

Sometimes there is some non-critical asynchronous operation that needs to happen but I don't want to wait for it to complete. In Tornado's coroutine implementation you can "fire & forget" an asynchronous function by simply ommitting the yield…
Mike N
  • 4,877
  • 3
  • 21
  • 19
117
votes
7 answers

How to set class attribute with await in __init__

How can I define a class with await in the constructor or class body? For example what I want: import asyncio # some code class Foo(object): async def __init__(self, settings): self.settings = settings self.pool = await…
uralbash
  • 2,267
  • 3
  • 21
  • 45
111
votes
4 answers

Difference between coroutine and future/task in Python 3.5?

Let's say we have a dummy function: async def foo(arg): result = await some_remote_call(arg) return result.upper() What's the difference between: import asyncio coros = [] for i in range(5): coros.append(foo(i)) loop =…
knite
  • 5,472
  • 5
  • 33
  • 51
108
votes
4 answers

asyncio.ensure_future vs. BaseEventLoop.create_task vs. simple coroutine?

I've seen several basic Python 3.5 tutorials on asyncio doing the same operation in various flavours. In this code: import asyncio async def doit(i): print("Start %d" % i) await asyncio.sleep(3) print("End %d" % i) return i if…
crusaderky
  • 2,131
  • 2
  • 15
  • 24
82
votes
10 answers

How to test Python 3.4 asyncio code?

What's the best way to write unit tests for code using the Python 3.4 asyncio library? Assume I want to test a TCP client (SocketConnection): import asyncio import unittest class TestSocketConnection(unittest.TestCase): def setUp(self): …
81
votes
6 answers

Simplest async/await example possible in Python

I've read many examples, blog posts, questions/answers about asyncio / async / await in Python 3.5+, many were complex, the simplest I found was probably this one. Still it uses ensure_future, and for learning purposes about asynchronous programming…
Basj
  • 29,668
  • 65
  • 241
  • 451
81
votes
6 answers

RuntimeError: This event loop is already running in python

I think I'm getting this error because my code calls asyncio.get_event_loop().run_until_complete(foo()) twice. Once from foo() and second time from function called by foo(). My question is then: why should this be a problem? Why should I even care…
wvxvw
  • 4,882
  • 7
  • 22
  • 44
78
votes
2 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
  • 3,862
  • 2
  • 29
  • 45
75
votes
1 answer

What is the core difference between asyncio and trio?

Today, I found a library named trio which says itself is an asynchronous API for humans. These words are a little similar with requests'. As requests is really a good library, I am wondering what is the advantages of trio. There aren't many articles…
Sraw
  • 14,837
  • 5
  • 37
  • 62
75
votes
6 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
  • 2,871
  • 6
  • 18
  • 38
72
votes
2 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
  • 755
  • 1
  • 5
  • 5
72
votes
3 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
  • 24,663
  • 9
  • 48
  • 87
1
2 3
99 100