Skip to content

thread safety #882

Open
Open
@stevengj

Description

@stevengj

If Julia's Threads.nthreads() > 1, we may want to do some more work to ensure thread safety. In particular:

  • Call PyEval_InitThreads() in __init__ (this is only needed for Python ≤ 3.6, and is a no-op in Python ≥ 3.7).
  • Acquire the GIL in the PyObject finalizer, by calling PyGILState_Ensure (returns an enum, i.e. a Cint in Julia) and PyGILState_Release. This is because the Julia GC may be called from threads other than the main thread, so we need to ensure that we hold Python's GIL before decref-ing.

We might also expose an API to acquire the GIL, but in general I would recommend that user code should only call Python from the main thread.

See this forum post and #881 for a possible test case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions