using Subprocess to avoid long-running task from disconnecting discord.py bot?

praw relies on the requests library, which is synchronous meaning that the code is blocking. This can cause your bot to freeze if the blocking code takes too long to execute. To get around this, a separate thread can be created that handles the blocking code. Below is an example of this. Note how blocking_function … Read more

Handling Timeouts with asyncio

Are there any practical differences between Options 1 and 2? No. Option 2 looks nicer and might be marginally more efficient, but their net effect is the same. I know run_until_complete will run until the future has completed, so since Option 1 is looping in a specific order I suppose it could behave differently if … Read more

aiohttp: rate limiting parallel requests

If I understand you well, you want to limit the number of simultaneous requests? There is a object inside asyncio named Semaphore, it works like an asynchronous RLock. semaphore = asyncio.Semaphore(50) #… async def limit_wrap(url): async with semaphore: # do what you want #… results = asyncio.gather([limit_wrap(url) for url in urls]) updated Suppose I make … Read more

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

The asyncio.run() documentation says: This function cannot be called when another asyncio event loop is running in the same thread. In your case, jupyter (IPython ≥ 7.0) is already running an event loop: You can now use async/await at the top level in the IPython terminal and in the notebook, it should — in most of the … Read more

How to terminate long-running computation (CPU bound task) in Python using asyncio and concurrent.futures.ProcessPoolExecutor?

How do I terminate such long running CPU-bound computations within a method? The approach you tried doesn’t work because the futures returned by ProcessPoolExecutor are not cancellable. Although asyncio’s run_in_executor tries to propagate the cancellation, it is simply ignored by Future.cancel once the task starts executing. There is no fundamental reason for that. Unlike threads, … Read more

Is it possible to run only a single step of the asyncio event loop

The missing of public method like loop.run_once() is intentional. Not every supported event loop has a method to iterate one step. Often underlying API has methods for creating event loop and running it forever but emulating single step may be very ineffective. If you really need it you may implement single-step iteration easy: import asyncio … Read more

Create generator that yields coroutine results as the coroutines finish

asyncio.as_completed() takes an iterable of coroutines or futures and returns an iterable of futures in the order that the input futures complete. Normally, you’d loop over its result and await the members from inside an async function… import asyncio async def first(): await asyncio.sleep(5) return ‘first’ async def second(): await asyncio.sleep(1) return ‘second’ async def … Read more