What is the best way to delay a few milliseconds within a nogil block, that's within a cdef function? time.sleep() seems to require the GIL, from my inspection of the cython-generated C code. I'm assuming this because it's incrementing and decrementing references, which I assume must be done within the GIL. Indeed, if I use the "nogil" suffix within my cdef call, I get the following error: "Constructing Python tuple not allowed without gil"
For context, in my application, I'm polling data from a piece of PCIe test equipment, using their published C API, in a thread. The C API does not support callbacks, just polling. I must poll it, quickly. Any data received is put into a pure-C queue implementation. Another thread will consume the queued data, and compute things with it, when there's data available. This second thread can be run more leisurely.
Is there a simple cross-platform C function that I could call, that just sleeps for a specified time? While I could cimport unistd.h's usleep() function, that's POSIX only. For Windows, I coulid call Sleep()
This is my best-guess of how it should be done, but it seems clunky.
IF UNAME_SYSNAME == "Windows":
cdef extern from "windows.h" nogil:
void Sleep(uint32_t dwMilliseconds)
cdef void cross_platform_sleep(uint32_t milliseconds) nogil:
Sleep(milliseconds)
ELSE:
from posix.unistd cimport usleep
cdef void cross_platform_sleep(uint32_t milliseconds) nogil:
usleep(milliseconds * 1000)
Is this a good way to accomplish it, or is there a better or simpler way?
Thanks!