Millet Porridge

English version of https://corvo.myseu.cn

0%

Learning About Coroutines and Asyncio

It started with reading Fluent Python; I found its asyncio chapter really hard to understand, so I looked through many references and learned a bit more about it.

What Is a Coroutine

This talk on Youtube is great — I hope you’ll watch it too, to see what coroutines really have to do with yield:

https://www.youtube.com/watch?v=M-UcUs7IMIM

I also came across some documentation on C++ and coroutines:

http://cpp.mimuw.edu.pl/files/await-yield-c++-coroutines.pdf

This is a kind of user-level thread that can effectively improve concurrency.

  • Context switching has overhead too, but compared with switching between threads it is negligible
  • Yet coroutines can access shared variables very easily, without any locking

Operating Systems

Batch processing systems: multi-program unordered scheduling; high system utilization, high throughput, long average turnaround time.

Advantages

  1. When system resources are busy, it can switch tasks to run
  2. Through per-minute monitoring, it avoids idle system resources
  3. By keeping utilization high, all tasks can pay their way in installments
  4. It allows the system to use different priorities for different tasks
  5. Ordinary programs run the program (processing logic) once per task execution, while a batch system can run the program (scheduler) once for many transactions.

The last point feels a bit redundant — reducing the number of main-program runs doesn’t fundamentally reduce the number of times the program logic runs.

Some Thoughts

In my view coroutines exist only to increase concurrency — that is, throughput — handling more events and more user requests at the same moment. But in actual operation all events just keep rotating, which means the average processing time per transaction gets longer.