Fine-grained locks

113 views
Skip to first unread message

Robert Kern

unread,
May 30, 2012, 7:49:26 AM5/30/12
to cython...@googlegroups.com
I'd like to write some Cython code in a separate thread "with nogil" but still
communicate from Python code on the main thread. Right now, my pure Python
prototype uses a Lock and a Condition to communicate with the worker thread. My
worker loop looks like this:

mutex = threading.Lock()
have_command = threading.Command(mutex)

with mutex:
while True:
have_command.wait()
cmd = get_command()
perform_command(cmd)

Does anyone have any recommendations for lock implementations to use? Windows
needs to work, and it would be ideal if it were cross-platform. Obviously, I
can't use threading.Lock because it is a Python object, and I don't want to grab
the GIL just to interact with that.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Robert Kern

unread,
May 30, 2012, 7:57:35 AM5/30/12
to cython...@googlegroups.com
On 5/30/12 12:49 PM, Robert Kern wrote:
> I'd like to write some Cython code in a separate thread "with nogil" but still
> communicate from Python code on the main thread. Right now, my pure Python
> prototype uses a Lock and a Condition to communicate with the worker thread. My
> worker loop looks like this:
>
> mutex = threading.Lock()
> have_command = threading.Command(mutex)
>
> with mutex:
> while True:
> have_command.wait()
> cmd = get_command()
> perform_command(cmd)
>
> Does anyone have any recommendations for lock implementations to use? Windows
> needs to work, and it would be ideal if it were cross-platform. Obviously, I
> can't use threading.Lock because it is a Python object, and I don't want to grab
> the GIL just to interact with that.

Never mind. I should have Googled more. It looks like cpython.pythread provides
what I need.

https://github.com/cython/cython/blob/master/Cython/Includes/cpython/pythread.pxd
Reply all
Reply to author
Forward
0 new messages