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
3
votes
1 answer

Why does `await asyncio.create_task()` behave different then when assigning it to a variable?

Why is there a different how tasks are run between tasks 1-3 and 4-6 in the code below? Code: import asyncio async def do_something(i, sleep): # No I/O here print("Doing... ", end="") print(await no_io_1(i, sleep)) async def no_io_1(i,…
Daniel
  • 3,092
  • 3
  • 32
  • 49
3
votes
1 answer

Why does the json() method of an aiohttp response require await?

I do not understand why resp.json() needs to be awaited. From my understanding async/await is useful when dealing with I/O. But when I call resp.json() in the example below, has the web request not already been processed with session.get() in the…
Daniel
  • 3,092
  • 3
  • 32
  • 49
3
votes
1 answer

How to approach returning an async function recursively

I've got a function that recursively attempts to retrieve information from a URL. If it receives a response other than 200, it tries again 3 times, ultimately returning None if nothing could be returned. My problem though is that when I run this…
AGH_TORN
  • 813
  • 17
  • 33
3
votes
1 answer

What is the purpose of asyncio.Queue() and how can I use it correctly?

I'm already familiar with python but I just started to learn about the topic of "asynchronous programming" and "io tasks". In the tutorial, I saw that asyncio.Queue() was used with something that related to producers and consumers, but I didn't…
user12418527
3
votes
2 answers

django 3.0 async orm

Since django 3.0 supports async I wonder what happens on database querying. I don't see any updates in the official documentation which I'm sure that syntax like this: b5.name = 'New name' b5.save() Will totally block the current thread, which…
deathangel908
  • 8,601
  • 8
  • 47
  • 81
3
votes
2 answers

Correctly catch aiohttp TimeoutError when using asyncio.gather

It is my first question here on Stack Overflow so I apologize if I did something stupid or missed something. I am trying to make asynchronous aiohttp GET requests to many api endpoints at a time to check the status of these pages: the result should…
Alpha Tauri
  • 33
  • 1
  • 5
3
votes
1 answer

Jupyterhub - ERROR:asyncio:Task exception was never retrieved

I'm not able to install Jupyterhub using pip. I tried reinstall, but no matter what, I'm still getting asyncio Error. Any idea what I'm doing wrong ? [root@localhost bin]# which python3 /usr/bin/python3 [root@localhost bin]# which…
jmt
  • 719
  • 1
  • 9
  • 28
3
votes
1 answer

Asynchronous Download of Files

This downloads updated fasta files (protein sequences) from a database, I've gotten this to work faster using asyncio compared to requests, however I'm not convinced the downloads are actually happening asynchronously. import os import…
Real Person
  • 79
  • 1
  • 6
3
votes
1 answer

How to handle CancelledError with python 3.8 unittest IsolatedAsyncioTestCase

I would like to ask question on handling asyncio.CancelledError from IsolatedAsyncioTestCase, which is provided for testing asyncio on py3.8 Given following testcases import unittest from unittest import IsolatedAsyncioTestCase import…
Mond Wan
  • 1,862
  • 2
  • 17
  • 21
3
votes
0 answers

aiomysql row locking when using multiple asyncio programs

I have a mysql table on aws mysql and as such my python asyncio program which is using aiomysql - https://github.com/aio-libs/aiomysql connects to a remote mysql database. My table shop has the following fields: id, names, product_id,…
Gandalf
  • 1
  • 29
  • 94
  • 165
3
votes
2 answers

Running an Asyncio loop within a time interval

Goal: run main() consisting of a bunch of asyncio functions between start_time and end_time import datetime as dt start_time, end_time= dt.time(9, 29), dt.time(16, 20) current_time() just keeps adding the current time to work space. This time is…
Saeed
  • 1,848
  • 1
  • 18
  • 26
3
votes
0 answers

Most elegant way to execute CPU-bound operations in asyncio application?

I am trying to develop part of system that has the following requirement: send health status to a remote server(every X seconds) receive request for executing/canceling CPU bound job(s)(for example - clone git repo, compile(using conan) it..…
nonamer92
  • 1,887
  • 1
  • 13
  • 24
3
votes
0 answers

Python async context manager needs to raise exception to caller

I have an context manager that is used like this: async with await MyContextManager() as cm: # cm is now doing heavy processing in the background until # it's told to stop... await asyncio.sleep(1) # simulate doing other things while …
seawolf
  • 2,147
  • 3
  • 20
  • 37
3
votes
1 answer

Utilizing asyncio generators and asyncio.as_completed

I have some code that I am using to scrape a url, parse the information, and then drop it into a DB using SQLAlchemy. I'm trying to do it asynchronously while limiting the maximum number of simultaneous requests. Here is my code: async def…
TMarks
  • 544
  • 4
  • 16
3
votes
2 answers

Use Qt Pyside2 with asyncio await syntax?

How to await this function (src ) in the main loop of pyside2: async def do_request(value): #asyncqt maybe possible #print("do request") await asyncio.sleep(value) #print("request finished") return value async def eventFilter(self,…
droid192
  • 2,011
  • 26
  • 43
1 2 3
99
100