jsstuball
jsstuball

Reputation: 4911

Can you add coroutine to front of event loop queue?

Is there a way to create a task but to have it specifically the next task run in the event loop?

Suppose I have an event loop currently running several low priority coroutines. Perhaps a few high priority API request tasks come along and I want to immediately asynchronously make these requests and then yield control back to the tasks previously in the loop.

I realize that the latency with a network request is orders of magnitude larger than a few CPU cycles saved by reordering the cooperative tasks in the loop, but nevertheless I am curious if there is a way to achieve this.

Upvotes: 5

Views: 1378

Answers (1)

user4815162342
user4815162342

Reputation: 154856

I want to immediately asynchronously make these requests and then yield control back to the tasks previously in the loop.

There is no way to do that in the current asyncio, where all runnable tasks reside in a non-prioritized queue.

But there is a deeper issue with the above requirement. Asynchronous tasks potentially yield control to the event loop at every blocking IO call, or more generally at every await. So "immediately" and "asynchronously" don't go together: a truly asynchronous operation cannot be immediate because it has to be suspendable, and when it is suspended, other tasks will proceed.

If you really want something to happen immediately, you need to do it synchronously. Other tasks will be blocked anyway because the synchronous operation will not allow them to run.

This is likely the reason why asyncio doesn't support task prioritization. By their very nature tasks execute in short slices that can be interleaved in arbitrary ways, so the order in which they execute should not matter in general. In cases when the order does matter, one is expected to use the provided synchronization devices.

Upvotes: 3

Related Questions