cython multithread behavior

17 views
Skip to first unread message

白隽

unread,
Sep 18, 2025, 8:24:12 AMSep 18
to cython-users
I encountered some problems when I use cython to wrap winfsp, and my cython code run in a thread not created in python. I found while the main thread blocking (like sleep, while True, or  WaitForSingleObject  ), then my code in cython running in winfsp thread will be blocking too. I don't know what happend. I create a small demo.
multithread.pyx
import time
import threading

def thread_run():
    while True:
        try:
            time.sleep(.5)
            print("thread run")
        except KeyboardInterrupt:
            print("key board interrupt")



def create_thread():
    th = threading.Thread(target=thread_run, daemon=True)
    th.start()
    try:
        while True:
            pass
        input("press enter to exit main thread\n")
    finally:
        print("main thread exit")
        while True:
            pass

test.py
import multithread
import threading


def create_thread():
    th = threading.Thread(target=multithread.thread_run, daemon=True)
    th.start()
    try:
        while True:
            pass
        # input("press enter to exit main thread\n")
    finally:
        print("main thread exit")
        while True:
            pass
if __name__ == "__main__":
    # create_thread()
    multithread.create_thread()

if the thread didn''t create in python,  main thread blocking will blocking the thread. The winfsp use WaitForSingleObject  to wait the thread stop, but cython code run in the thread was blocking, cause a dead lock.

da-woods

unread,
Sep 18, 2025, 1:22:31 PMSep 18
to cython...@googlegroups.com

The issue will be the global interpreter lock.

Python allows the global interpreter lock to switch after a certain number of bytecode instructions (roughly). Cython code won't release the global interpreter lock unless you specifically tell it to (or unless a Python call triggers it)

So you'd do:

```
while True:
    with nogil:  # Note that this doesn't work well on PyPy for some reason
        pass
# or
with nogil:
    WaitForSingleObject(...)
```

I believe time.sleep should release the global interpreter lock itself though.

--

---
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 visit https://groups.google.com/d/msgid/cython-users/766dedeb-6202-4156-a39d-3de6803a25f7n%40googlegroups.com.

白隽

unread,
Sep 19, 2025, 12:20:47 AMSep 19
to cython...@googlegroups.com
Thank you for your reply.Today I found time.sleep can release GIL,so I rewrite FspSystemStopDispatcher, set WaitForSingleObject a short timeout and time.sleep in a loop.Now I can try to modify it to 
with nogil:
    WaitForSingleObject(...).
It runs well and elegantly.
Thank u again.

da-woods <dw-...@d-woods.co.uk>于2025年9月19日 周五01:22写道:

白隽

unread,
Sep 19, 2025, 4:25:14 AMSep 19
to cython...@googlegroups.com
Finally,I used 
with nogil:
    FspFileSystemStopDispatcher()
That’s useful!

da-woods <dw-...@d-woods.co.uk>于2025年9月19日 周五01:22写道:

The issue will be the global interpreter lock.

Reply all
Reply to author
Forward
0 new messages