Best way to delay a few milliseconds within nogil block?

357 views
Skip to first unread message

njan...@gmail.com

unread,
Oct 2, 2020, 2:06:53 PM10/2/20
to cython-users
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!

Stefan Behnel

unread,
Oct 3, 2020, 1:50:17 AM10/3/20
to cython...@googlegroups.com
njan...@gmail.com schrieb am 02.10.20 um 19:28:
I think that's the right idea, but you should do it in C using #ifdef's,
and not in Cython.

Since this is a good example for platform adaptation, I took the liberty to
add it to the documentation:

https://cython.readthedocs.io/en/latest/src/userguide/external_C_code.html#including-verbatim-c-code

Stefan

Robert Bradshaw

unread,
Oct 3, 2020, 1:54:13 AM10/3/20
to cython...@googlegroups.com
I ran into this issue as well and created https://pypi.org/project/cytime

--

---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/b0f14da4-2ec2-95dc-75de-d0716b137716%40behnel.de.

Stefan Behnel

unread,
Oct 3, 2020, 3:40:49 AM10/3/20
to cython...@googlegroups.com
Robert Bradshaw schrieb am 03.10.20 um 07:53:
> I ran into this issue as well and created https://pypi.org/project/cytime

There's also

https://github.com/cython/cython/issues/3733

https://github.com/cython/cython/pull/3767

Stefan

Neil Jansen

unread,
Oct 3, 2020, 11:48:41 AM10/3/20
to cython...@googlegroups.com
Thanks Stefan and Robert.  cytime looks like a great short-term fix and long-term, hopefully that pull request will get merged in. 

I had another similar question. I can't use a threading.RLock outside of the GIL either.  I'm not a C (or C++ for that matter) expert but I don't recall there being any standard library implementations of mutexes or rlocks in the C standard library.  The Cython library compiles to C, not C++, so I'm not keen on using the C++ standard library threading or locks although I might be inclined to use them if no other choices are available.  So I have the same problem here.  I'd like to use a mutex outside of the GIL to protect my custom queue implementation between the producer thread and the consumer thread.  And at a minimum it would need to compile to Windows and Linux.  Any ideas or suggestions?

--

---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/SOFcHsnmIV4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/07de0a7c-3f68-6333-c85b-ae6e85866275%40behnel.de.
Reply all
Reply to author
Forward
0 new messages