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
322
votes
6 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
  • 8,089
  • 10
  • 32
  • 61
311
votes
10 answers

multiprocessing vs multithreading vs asyncio

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,…
291
votes
6 answers

asyncio.gather vs asyncio.wait (vs asyncio.TaskGroup)

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). Since Python 3.11 there is yet another similar feature,…
Claude
  • 8,806
  • 4
  • 41
  • 56
219
votes
14 answers

Asynchronous method call in Python?

I was wondering if there's any library for asynchronous method calls in Python. It would be great if you could do something like @async def longComputation(): token = longComputation() token.registerCallback(callback_function) #…
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
218
votes
10 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
  • 41,386
  • 99
  • 383
  • 673
200
votes
5 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
  • 6,395
  • 4
  • 24
  • 21
200
votes
14 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
  • 3,219
  • 5
  • 25
  • 45
143
votes
8 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
  • 8,089
  • 10
  • 32
  • 61
136
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,552
  • 3
  • 20
  • 28
135
votes
8 answers

"asyncio.run() cannot be called from a running event loop" when using Jupyter Notebook

I would like to use asyncio to get webpage html. I run the following code in jupyter notebook: import aiofiles import aiohttp from aiohttp import ClientSession async def get_info(url, session): resp = await session.request(method="GET",…
Chan
  • 3,605
  • 9
  • 29
  • 60
132
votes
5 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
  • 6,033
  • 6
  • 38
  • 54
126
votes
11 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): …
125
votes
10 answers

How to limit concurrency with Python asyncio?

Let's assume we have a bunch of links to download and each of the link may take a different amount of time to download. And I'm allowed to download using utmost 3 connections only. Now, I want to ensure that I do this efficiently using…
Shridharshan
  • 1,307
  • 2
  • 9
  • 9
119
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
  • 18,892
  • 11
  • 54
  • 87
114
votes
8 answers

RuntimeError: There is no current event loop in thread in async + apscheduler

I have a async function and need to run in with apscheduller every N minutes. There is a python code below URL_LIST = ['', '', '', ] def demo_async(urls): """Fetch list of web pages…
Valera Shutylev
  • 1,145
  • 2
  • 7
  • 6
1
2 3
99 100