How to use locks in nopython mode?

Which lock implementation can be used when using @njit(nogil=True)?

It refuses to compile with threading.Lock() -

import threading
from numba import njit


lock = threading.Lock()


@njit(nogil=True)
def child():
    lock.acquire()


child()
Untyped global name 'lock': cannot determine Numba type of <class '_thread.lock'>

File "xxx.py", line 67:
def child():
    lock.acquire()
    ^
1 Like

Hi @devxpy

There are no locks supported in nopython mode. If you want to use a lock you’d need to use an object mode block .

Hope this helps?

2 Likes

Thanks!

Just curious - this can potentially be done by wrapping the semaphore.h library shipped with the OS using numba ffi?

Is that a reasonable way to do this?

I’m looking into sane ways of extending numba beyond its inbuilts, is cython + ffi a good approach?

Sorry for firing so many questions at you, hope you’re doing good :slight_smile:

1 Like

It’s possible to bind to exported symbols in libraries by using ctypes, it’s also possible to use cffi, both have restricted but present support for use in Numba nopython mode compiled code (click associated links for docs).

Out of interest, what is the purpose of the lock?

1 Like

Just wondering how far I can take nopython + nogil. It looks like a clever solution to python’s GIL problem!

Initial tests yield 100% CPU across all cores, never thought I’d see this day with the GIL :smiley:

1 Like