python - How to set result of future\add to queue from another thread in Cython? -
i have c++ dll works multiple threads. wrapped library cython , created special receiver callback-function, must adds results asyncio.queue.
cdef void __cdecl newmessage(char* message) nogil:
i marked nogil, callback calls thread. in callback use:
with gil: print("hello") # instead adding queue. print("hello") more simple operation demostrate problem
and got deadlock here. how resolve it?
c++ callback declaration (header):
typedef void (*receiver_t)(char*); void setreceiver(receiver_t new_rec);
cpp:
static receiver_t receiver = nullptr; void setreceiver(receiver_t new_rec) { printf("setted%i\n", (int)new_rec); = 123; if (new_rec != nullptr) receiver = new_rec; }
cython code:
cdef extern "teamspeak3.h": ctypedef void (*receiver_t) (char*) nogil cdef void __cdecl setreceiver(receiver_t new_rec) nogil cdef void __cdecl newmessage(char* message) nogil: gil: print("hello") setreceiver(newmessage)
full code: .h http://pastebin.com/ztcjc6na
.cpp http://pastebin.com/meyga8im
this bit of guess have cython/c/c++ loop running that's holding gil , never releasing it. callback forced wait forever it.
in normal python code gil released every few instructions if thread waiting it. in cython doesn't happen automatically. 1 way ensure happen every to loop:
while true: # ... stuff nogil: pass
this ensures gil released once per loop.
unfortunately it's not obvious me have main loop. wonder if it's inside connect
in pyteamspeak3
class, , perhaps changing definition of connect to:
def connect(self): nogil: self.thisptr.connect()
might help?
Comments
Post a Comment