yglukhov / nimpy

Nim - Python bridge
MIT License
1.47k stars 62 forks source link

How to use it with multithreading? #304

Closed cvanelteren closed 3 days ago

cvanelteren commented 1 month ago

I am playing around with Nim and python where I need to access python objects from different threads. In cython we get a handy with gil operator. Is there a way to access python objects in a similar way under Nimpy? Could you provide a minimal example?

Cheers! C

cvanelteren commented 1 month ago

In particular I have this example I am running with:

code ```Nim import nimpy{.all.}, dynlib import nimpy/py_lib {.all.} import malebolgia type PyGILState_STATE = enum PyGILState_LOCKED, PyGILState_UNLOCKED {.pragma: pyfunc, cdecl, gcsafe.} # initPyLibIfNeeded() let m = pythonLibHandleForThisProcess() nx = pyImport("networkx") PyGILState_Ensure = cast[proc(): PyGILState_STATE {.pyfunc.}](m.symAddr("PyGILState_Ensure")) PyGILState_Release = cast[proc(s: PyGILState_STATE) {.pyfunc.}](m.symAddr("PyGILState_Release")) template withGIL*(code) = let state = Py_GILState_Ensure() code Py_GILState_Release(state) withGil: echo "in gil" proc createG(idx: int) = echo idx withGil: var g = nx.path_graph(3) var master = createMaster() master.awaitAll: for idx in 0..1000: master.spawn createG(idx) echo "done" unloadLib(m) ```
cvanelteren commented 3 days ago

Figure it out, see for explanation here