lexus2k / tinyproto

Tiny Software Protocol for communication over UART, SPI, etc
GNU General Public License v3.0
225 stars 51 forks source link

Improve python bindings performance #30

Closed chenlijun99 closed 2 years ago

chenlijun99 commented 2 years ago

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.