I will try to explain what I am doing with an example, say I am building a weather client. The browser sends a message over websocket, eg: The server queries the weather every 5 minutes and updates the browser back with the latest data. Now the browser could send another message, eg: Now I the server should STOP updating the
Tag: python-asyncio
Asynchronous code failure when connecting to redis
I created a small class to perform basic operations with redis, using aioredis. And a test handler to call the write/read operation for redis But get error I guess the problem could be that the asynchronous code has to be run through an event loop But I can’t understand how I can build this logic into my code Answer Your
Running a loop while using telethon
I’m making a telegram channel admin control script that needs checking channel in a loop. I also want some message handlers checking for messages. Here is a part of my code: If i use client.loop.run_until_complete(main()), message handler won’t work but main runs prefectly. and if I use client.run_until_disconnected() and run main in another loop using another thread, it stucks at
Python – getting lost around async
As the title said – I’m having a problem with async stuff. What I’m trying to achieve is written under every function. Sadly in this state of code I’m getting and error: TypeError: object StreamReader can’t be used in ‘await’ expression and at the end RuntimeError: Event loop is closed I was googling for a while and didn’t really find
How to collect wait()’d co-routines in a set?
I have been attempting to generate a ping scan that uses a limited number of processes. I tried as_completed without success and switched to asyncio.wait with asyncio.FIRST_COMPLETED. The following complete script works if the offending line is commented out. I’d like to collect the tasks to a set in order to get rid of pending = list(pending) however pending_set.union(task) throws
How to call synchronous function(s) from async functions in safe manner
What can occur if one or more workers call ‘Synchronous function’ simultaneously ? Maybe one or more workers become blocked for a while ? Answer Short answer: If you call a synchronous (blocking) function from within an async coroutine, all the tasks that are concurrently running in the loop will stall until this function returns. Use loop.run_in_executor(…) to asynchronous run
How to determine the optimal amount of buffer size with asyncio/aiohttp
How do we decide the optimal parameter for read() when working with asyncio in python? 12 bytes? 100 bytes? Answer How do we decide the optimal parameter for read() when working with asyncio in python? 12 bytes? 100 bytes? You can safely choose a much larger number than that. If the number is too small (e.g. just 1), your loop
Python asyncio cancel unawaited coroutines
So given a bit of a complex setup, which is used to generate a list of queries to be run semi-parallel (using a semaphore to not run too many queries at the same time, to not DDoS the server). i have an (in itself async) function that creates a number of queries: Now this works very nicely, executing exactly as
Asyncio stream process data with pipe between two subprocesses
Hello I can’t find a solution for the example here. I found https://stackoverflow.com/a/36666420/6089311 but there is no stream reading. I’d like to run two subprocesses independent of python program (running in the background). The first subprocess feeds the second process through the pipe and I want to do some processing with the stdout lines by stream way. The example bellow
asyncio run_until_complete does not wait that all coroutines finish
I am making my first steps in Python and I have a bit of struggle trying to understand why I do not have the expected result with this one. Here is what I am trying to achieve : I have a function that consumes an API. While waiting for the API to answer and given that I am going through