C extension code that blocks should release the Python GIL to allow other Python threads to run. See https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock.
I added also a Python loopback example, similar to the C++ loopback example. Obviously the Python loopback example can also communicate with the C++ loopback example.
I split the addition of the Python loopback example and the performance improvement into two commits, so that you can more easily evaluate the benefits of the performance improvements.
Note that without correctly managing the GIL, threads that block in the C-extension code cannot be switched out and some ad-hoc time.sleep() are required to force a thread to yield. By correctly releasing the GIL when appropriate, those time.sleep() are no more required.
C extension code that blocks should release the Python GIL to allow other Python threads to run. See https://docs.python.org/3/c-api/init.html#thread-state-and-the-global-interpreter-lock. I added also a Python loopback example, similar to the C++ loopback example. Obviously the Python loopback example can also communicate with the C++ loopback example. I split the addition of the Python loopback example and the performance improvement into two commits, so that you can more easily evaluate the benefits of the performance improvements. Note that without correctly managing the GIL, threads that block in the C-extension code cannot be switched out and some ad-hoc
time.sleep()
are required to force a thread to yield. By correctly releasing the GIL when appropriate, thosetime.sleep()
are no more required.