Skip to content
Advertisement

Tag: python-asyncio

How to suppress “coroutine was never awaited” warning?

All search results on “coroutine was never awaited” are for people who were either trying to fire-and-forget or actually did forget to await. This is not my case. I want to use a coroutine the same way I often use generators: I’m creating it here while I have all the variables handy, but I’m not sure yet whether I’ll ever

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

I would like to use asyncio to get webpage html. I run the following code in jupyter notebook: However, it returns RuntimeError: asyncio.run() cannot be called from a running event loop What is the problem? How to solve it? Answer The asyncio.run() documentation says: This function cannot be called when another asyncio event loop is running in the same thread.

Non-blocking launching of concurrent coroutines in Python

I want to execute tasks asynchronously and concurrently. If task1 is running when task2 arrives, task2 is started right away, without waiting for task2 to complete. Also, I would like to avoid callbacks with the help of coroutines. Here’s a concurrent solution with callbacks: Its output: And here’s my effort to get rid of callbacks: Output: In this case initial

AttributeError: module ‘asyncio’ has no attribute ‘create_task’

I’m trying to asyncio.create_task() but I’m dealing with this error: Here’s an example: Out: So I tried with the following code snippet (.ensure_future()) instead, without any problem: Out: What’s wrong? [NOTE]: Python 3.6 Ubuntu 16.04 [UPDATE]: With borrowing from @user4815162342 Answer, my problem solved: Answer The create_task top-level function was added in Python 3.7, and you are using Python 3.6.

The tasks from asyncio.gather does not work concurrently

I want to scrape data from a website concurrently, but I found that the following program is NOT executed concurrently. However, this program starts to download the second content only after the first one finishes. If my understanding is correct, the await keyword on the await return_soup(url) awaits for the function to be complete, and while waiting for the completion,

Wait for timeout or event being set for asyncio.Event

I have a class with a method that looks like this: The idea is that several of these are run in their own thread and at some point one thread does stop_event.set(), which naturally stops all others. I want to switch to asyncio for this, because the tasks in run are mostly sleeping and doing IO. Thus, I got to:

Strange behaviour when task added to empty loop in different thread

I have an app which adds coroutines to an already-running event loop. The arguments for these coroutines depend on I/O and are not available when I initially start the event loop – with loop.run_forever(), so I add the tasks later. To demonstrate the phenomenon, here is some example code: The strange behaviour is that everything works as expected when there

Advertisement