All Questions
Tagged with python-asyncio python-multithreading
197 questions
33
votes
4
answers
28k
views
python asyncio, how to create and cancel tasks from another thread
I have a python multi-threaded application. I want to run an asyncio loop in a thread and post calbacks and coroutines to it from another thread. Should be easy but I cannot get my head around the ...
30
votes
3
answers
38k
views
Why coroutines cannot be used with run_in_executor?
I want to run a service that requests urls using coroutines and multithread. However I cannot pass coroutines to the workers in the executor. See the code below for a minimal example of this issue:
...
19
votes
3
answers
27k
views
How to send server-side events from python (fastapi) upon calls to a function that updates the backend state
I have the following problem: given a backend running fastapi, that has a streaming endpoint, which is used to update the frontend, I want to send these updates every time the function that updates ...
17
votes
3
answers
7k
views
Can concurrent.futures.Future be converted to asyncio.Future?
I'm practicing asyncio after writing multithreaded code many years.
Noticed something which i find it strange. Both in asyncio and in concurrent there is a Future object.
from asyncio import Future
...
14
votes
4
answers
17k
views
asyncio: Wait for event from other thread
I'm designing an application in Python which should access a machine to perform some (lengthy) tasks. The asyncio module seems to be a good choice for everything that is network-related, but now I ...
9
votes
1
answer
6k
views
Python Asyncio errors: "OSError: [WinError 6] The handle is invalid" and "RuntimeError: Event loop is closed" [duplicate]
I am having some difficulties with properly with my code, as I get the following error after my code finishes executing while debugging on VSCode:
Exception ignored in: <function ...
9
votes
2
answers
5k
views
python non blocking write csv file
I am writing some python code to do some calculation and write the result to file. Here is my current code:
for name, group in data.groupby('Date'):
df = lot_of_numpy_calculations(group)
...
9
votes
1
answer
2k
views
How can asyncio ever not be thread safe considering the GIL?
The asyncio docs read:
Most asyncio objects are not thread safe. You should only worry if you access objects outside the event loop.
Could someone explain this or give an example of how misuse of ...
9
votes
1
answer
5k
views
Multi-thread support for python asyncio gRPC clients
I have an asyncio gRPC client that is used in a multithreaded environment. When multiple threads connect to the service via the client simultaneously, I see a stream of the following errors:
2021-01-...
9
votes
2
answers
5k
views
Debugging an async program in the pycharm debugger
My request is to get debugging data from the debugger and pycharm prompt:
Lets say I have an-old-style python synchronous program with a bug:
def listdir(self, remote_path):
with ssh....
8
votes
2
answers
2k
views
Is there a way to tell if an asyncio event loop is at full capacity?
I'm running an asyncio application which needs more than one event loop to service a large number of IO operations (upward of a thousand simultaneously). The event loops run in a separate thread and ...
6
votes
1
answer
8k
views
Python asyncio run_coroutine_threadsafe never running coroutine?
I'm not sure what I'm doing wrong here, I'm trying to have a class which contains a queue and uses a coroutine to consume items on that queue. The wrinkle is that the event loop is being run in a ...
6
votes
3
answers
1k
views
python: proper way to run an async routine in a pytest fixture?
The test below passes, but I have doubts that I am using asyncio correctly:
The code mixes asyncio and threading
The test is passing but never exits (probably because the "loop....
5
votes
1
answer
3k
views
How to shutdown process with event loop and executor
Consider the following program.
import asyncio
import multiprocessing
from multiprocessing import Queue
from concurrent.futures.thread import ThreadPoolExecutor
import sys
def main():
executor = ...
5
votes
1
answer
5k
views
run infinite loop in asyncio event loop running off the main thread
I wrote code that seems to do what I want, but I'm not sure if it's a good idea since it mixes threads and event loops to run an infinite loop off the main thread. This is a minimal code snippet that ...