Best practice for marking a function as nogil

22 views
Skip to first unread message

Kai-Hsun Chen

unread,
Feb 10, 2025, 2:01:44 PMFeb 10
to cython-users
Hi team,

I read the doc Cython and the GIL. It mentions that

```
cdef void some_func() noexcept nogil:
    ....
```

> Be aware that this does not release the GIL when calling the function. It merely indicates that a function is suitable for use when the GIL is released. It is also fine to call these functions while holding the GIL.

I am wondering what the best practice is for marking a function as nogil. Is it okay to always add nogil in the function definition and use `with gil` to acquire the GIL when necessary? It seems that there is no additional overhead because nogil doesn't release the GIL.

Take two functions from ray-project/ray as examples. In example [1], it acquires the GIL by calling with gil immediately upon entering the function marking as nogil, and only the last line is outside the GIL. In example [2], the entire function is under with gil. Should I remove nogil, and what is the reason? Thanks!



Best,
Kai-Hsun

da-woods

unread,
Feb 10, 2025, 3:54:57 PMFeb 10
to cython...@googlegroups.com

There isn't a single best practice because most combinations have their uses

cdef void some_func() nogil:
    ...  # code goes here

This is useful when you don't really care whether the caller has the GIL. You might do that if you've got a short function that you call from a lot of places. However, you definitely don't want to pay the cost of releasing and re-acquiring the GIL .

cdef void some_func() nogil:
    with nogil:
        ... # code goes here

This is useful when the contents are long-running enough that it's definitely worth releasing the GIL.

cdef void some_func() nogil:
    ... # code
    with gil:
       ... # python code
    ... # code

This is a useful thing to do if some of the code requires the GIL. However, if most of the code requires the GIL then your callers may think it's misleading.

> Is it okay to always add nogil in the function definition and use `with gil` to acquire the GIL when necessary?

No - functions taking or returning Python objects do require the GIL so it isn't always OK.

> It seems that there is no additional overhead because nogil doesn't release the GIL

There's always a cost to releasing, reacquiring, or working out if you have the GIL. So it's probably worth minimizing the number of times you do it.

For the two Ray functions, labeling them as nogil looks slightly pointless to me. However, if they're being passed around in external code as C callbacks then it might make sense.

--

---
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/082d02bc-3557-44ed-9a7c-8513ab13f675n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages