setting nogil at cimport

33 views
Skip to first unread message

Björn Dahlgren

unread,
Sep 23, 2016, 2:17:48 PM9/23/16
to cython-users
Hi,

Here's an odd situation, I have a C++ function which takes a template typename argument (a C++ class).
Depending on what I specialize this function as, it may or may not touch any python objects (depending on whether that class
holds any python object references).

I guess there is no way to indicate at cimport time whether I consider it to be 'nogil' or not?
I can just create another copy of the pxd of course: myobj.pxd & myobj_nogil.pxd - perhaps that is easier here?

Regards,
Björn

Robert Bradshaw

unread,
Sep 23, 2016, 5:45:16 PM9/23/16
to cython...@googlegroups.com
No, this isn't possible. You're probably better off generating two copies of the pxd. (That is, assuming you actually take advantage of releasing the gil in the one cases where you can--if not then you don't even need to worry about it.)

--

---
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Björn Dahlgren

unread,
Sep 23, 2016, 6:49:54 PM9/23/16
to cython-users


On Friday, 23 September 2016 23:45:16 UTC+2, Robert Bradshaw wrote:
No, this isn't possible. You're probably better off generating two copies of the pxd. (That is, assuming you actually take advantage of releasing the gil in the one cases where you can--if not then you don't even need to worry about it.)


Thanks. Yes, I was hoping to use prange. Is it enough to just do:

with nogil:
    c_callback(&my_numpy_array[0])

or does c_callback need to be declared "nogil" in my pxd as well?

Stefan Behnel

unread,
Sep 26, 2016, 1:56:13 AM9/26/16
to cython...@googlegroups.com
Cython validates your code for GIL dependencies inside of nogil sections,
so no, this will require the declaration to be there.

I'd be careful with keeping Python object reference in a C++ container,
though. It prevents automatic reference cleanup on deallocation, so you're
very much on your own here.

Stefan

Reply all
Reply to author
Forward
0 new messages