[Python] Python coroutine context switch is slower than thread context switch?

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to measure context switch time of coroutine and python thread by having 2 threads waiting for a event that is set by the other thread. Threading context switch takes 3.87 µs, which matches my expectation as OS context switch does takes a few thousands of instructions. The coroutine version's context switch is 14.43 µs, which is surprising to me as I was expecting coroutine context switch to be magnitude faster. Is it a Python coroutine issue is my program wrong?

    Code can be found in this gist

    Rewriting the program in rust gives more reasonable results: coro: 163 ns thread: 1989 ns

    Continue reading...

Compartilhe esta Página