# Nogil with jitclass?

**URL:** <https://numba.discourse.group/t/nogil-with-jitclass/394>\
**Category:** Community Support\
**Created:** [December 11, 2020, 2:27pm UTC](https://numba.discourse.group/t/nogil-with-jitclass/394 "2020-12-11T14:27:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![wielandbrendel](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/wielandbrendel/32/173_2.png) [@wielandbrendel](https://numba.discourse.group/u/wielandbrendel)\
**Post date:** [December 11, 2020, 2:27pm UTC](https://numba.discourse.group/t/nogil-with-jitclass/394/1 "2020-12-11T14:27:09Z")

</div>

Dear all,  
I am trying to use joblib parallel (with threading backend) to parallelize a set of calls to a jitclass. That doesn’t seem to work, probably because jitclass doesn’t release the GIL. Is there a way to pass something like nogil=True to jitclass, or is that generally not supported?

Thanks!

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [December 11, 2020, 5:21pm UTC](https://numba.discourse.group/t/nogil-with-jitclass/394/2 "2020-12-11T17:21:06Z")

</div>

Hi @wielandbrendel,

From memory, `@njit` flags in general cannot be passed to `jitclass`, construction is “automatic” and uses `njit` directly.

> <https://github.com/numba/numba/blob/release0.52/numba/experimental/jitclass/base.py#L110>

  

> <https://github.com/numba/numba/blob/release0.52/numba/experimental/jitclass/base.py#L217-L229>

Hope that answers your question?

---

<div class="post-metadata">

**Author:** ![wielandbrendel](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/wielandbrendel/32/173_2.png) [@wielandbrendel](https://numba.discourse.group/u/wielandbrendel)\
**Post date:** [December 12, 2020, 8:52am UTC](https://numba.discourse.group/t/nogil-with-jitclass/394/3 "2020-12-12T08:52:03Z")

</div>

Thanks so much! So the short answer is: it is currently not possible to use jitclass without GIL, right?

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [December 14, 2020, 10:57am UTC](https://numba.discourse.group/t/nogil-with-jitclass/394/4 "2020-12-14T10:57:04Z")

</div>

Yes, this is the case.

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [January 5, 2021, 12:08pm UTC](https://numba.discourse.group/t/nogil-with-jitclass/394/5 "2021-01-05T12:08:43Z")

</div>

I’m sure I’m misunderstanding, but I had thought that a nogil njit function calling another not-declared-nogil njit function would _ **not** _ reacquire the gil- is that correct? Sample pseudocode:

```python
@njit(nogil=False)
def has_gil():
    # is gil owned here?
    pass

@njit(nogil=True)
def no_gil():
    has_gil()

no_gil()

```

---

<div class="post-metadata">

**Author:** ![adriangb](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/adriangb/32/322_2.png) [@adriangb](https://numba.discourse.group/u/adriangb)\
**Post date:** [May 15, 2021, 10:24pm UTC](https://numba.discourse.group/t/nogil-with-jitclass/394/6 "2021-05-15T22:24:23Z")

</div>

Could this be added without too much work? I envision the following:

```python
from numba.experimental import jitclass
import numba as nb

@jitclass()
class Class1:
    a: float

    def __init__ (self, a: float) -> None:
        self.a = a

    @nb.jit(nogil=True, nopython=True, fastmath=True, parallel=True)
    def score(self, thing: float) -> float:
        res = thing
        for _ in nb.prange(10):
            res += self.a
        return res

c = QualDiffScoring(1)
c.score(1) # works

```

Which from some preliminary monkey patching can be achieved with:

```python
    jit_methods, methods, props, static_methods, others = {}, {}, {}, {}, {}
    for k, v in clsdct.items():
        if isinstance(v, pytypes.FunctionType):
            methods[k] = v
        elif isinstance(v, property):
            props[k] = v
        elif isinstance(v, staticmethod):
            static_methods[k] = v
        elif isinstance(v, dispatcher.Dispatcher):
            jit_methods[k] = v
        else:
            others[k] = v

    ...

    jit_methods.update({k: njit(v) for k, v in methods.items()})

```

In `numba.experimental.jitclass.base.py`.

For my use case, I have many classes with a `score` method and so it’s helpful to namespace them, as opposed to having:

```python
@jitclass()
class Class1:
    a: float

    def __init__ (self, a: float) -> None:
        self.a = a

@nb.jit(nogil=True, nopython=True, fastmath=True, parallel=True)
def score_class1(c: Class1, thing: float) -> float:
    res = thing
    for _ in nb.prange(10):
        res += c.a
    return res

```

And then having to pass both things around in tandem.
