asynchronous

Why asynchronous sleep task and cpu-bound task cannot proceed concurrently?

Why asynchronous sleep task and cpu-bound task cannot proceed concurrently? Question: I need to send HTTP requests and do some CPU intensive task while waiting for the response. I tried to mock the situation with an asyncio.sleep and a CPU task below: import asyncio async def main(): loop = asyncio.get_event_loop() start = loop.time() task = …

Total answers: 2

Multithreading in the program does not work for me, what could be the problem?

Multithreading in the program does not work for me, what could be the problem? Question: I’m trying to make my account autoregister on 1 of the servicers using python. I decided to resort to multithreading. I enter the number of threads I need into the console, but they do not start asynchronously. At first, the …

Total answers: 1

Why async function binds name incorrectly from the outer scope?

Why async function binds name incorrectly from the outer scope? Question: Here is an async function generator by iterating a for loop. I expected this closure to respect the names from the outer scope. import asyncio coroutines = [] for param in (1, 3, 5, 7, 9): async def coro(): print(param ** 2) # await …

Total answers: 2

Can I use sync_to_async for any function in python?

Can I use sync_to_async for any function in Python? Question: Background: I’m working on a Discord bot that uses requests. The requests are asynchronous, so I’m using the library asgiref.sync. (I know I obviously can’t use this function for asynchronous functions.) I implemented sync_to_async into all the requests and things that may take long to …

Total answers: 2

Cannot load table object with sqlalchemy asyncio

Cannot load table object with sqlalchemy asyncio Question: I am attempting to load a table using sqlalchemy asyncio. The synchronous way I would run it is as follows: connect_string = ‘db_handle://user:password@db_address:port/database’ # where db_handle is postgressql+psycopg2 engine = create_engine(connect_string) table = Table(table, metadata, autoload=True, autoload_with=engine) None of the solutions I implemented allow me (sqlalchemy core …

Total answers: 1

AttributeError: 'coroutine' object has no attribute 'edit'

AttributeError: 'coroutine' object has no attribute 'edit' Question: I am making a discord bot: There is an async function that corresponds to a slash command. I have another function called count(): async def count(n): for i in range(n): yield i and in the slash command function: msg = ctx.respond("") for i in count(n): await msg.edit(i) …

Total answers: 2

Python Async Limit Concurrent coroutines per second

Python Async Limit Concurrent coroutines per second Question: My use case is the following : I’m using python 3.8 I have an async function analyse_doc that is a wrapper for a http request to a web service. I have approx 1000 docs to analyse as fast as possible. The service allows for 15 transaction per …

Total answers: 1

Python multiprocessing: calling methods and passing objects in asynchronous calls

Python multiprocessing: calling methods and passing objects in asynchronous calls Question: I am trying to accomplish two things with apply_async (https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.AsyncResult) call: (i) Call a class method (ii) Pass an object as param I have the following baseline code so far: import multiprocessing as mp class myClass(): def __init__(self, id): self.id = id self.val = …

Total answers: 1

How do I terminate an asyncio loop in these conditions?

How do I terminate an asyncio loop in these conditions? Question: I am creating a python code that does the following: 1 – creates an asynchronous function called "nums" than prints random numbers from 0 to 10 every 2 seconds. 2 – execute a function that prints "hello world" every 5 seconds. 3 – if …

Total answers: 1