[Twisted-Python] reactor and thread
Hi all, I have a project where I writing the "server" (on win xp) and I want to communicate with a client, so twisted it's all I need. Into the "server" I have also a python bind to a dll (with ctypes) and I see strange problems (windows crashes) when I call a dll function inside a thread. My thread it's a twisted "Protocol". My question is: it is possible to call the function from/to a specific thread? So somethink like: class MyDll: def funct(self, callBack): #do the work reactor.call_in_main_thrad(callBack) tid = create_a_thread_and_load_MyDll() # where I make the dll instance and all the work need into a thread #into a someone function into the main thread def callBack(): # a simple callback reactor.callIntoThread(tid, funct, callBack) Is this possible with twisted? (Does I'm dreaming?) P.s. If I try with the wx "reactor" (so wx.MainLoop), I see no problem! In that case, I instance the dll into the class __init__ (inherit from wx.Frame) and into a someone event (i.e. Button click) I call a dll funct and all work without crash... Thanks, Michele
Hi Michele,
My question is: it is possible to call the function from/to a specific thread?
you could use deferToThread() to run a task in a separate thread and get the results in the main thread. here are the docs: http://twistedmatrix.com/documents/current/api/twisted.internet.threads.html... here is a small example which asks for user input in a separate thread: -------- from twisted.internet import reactor, defer, threads def input(): return threads.deferToThread(blocking_task) def blocking_task(): print "working... enter something to end task" return raw_input() def got_result(data): print "the long-running task results in:", data reactor.stop() input().addCallback(got_result) reactor.run() -------- is this what you want to do? On 6/13/07, Michele Petrazzo - Unipex srl <michele.petrazzo@unipex.it> wrote:
Hi all, I have a project where I writing the "server" (on win xp) and I want to communicate with a client, so twisted it's all I need. Into the "server" I have also a python bind to a dll (with ctypes) and I see strange problems (windows crashes) when I call a dll function inside a thread. My thread it's a twisted "Protocol". My question is: it is possible to call the function from/to a specific thread? So somethink like:
class MyDll: def funct(self, callBack): #do the work reactor.call_in_main_thrad(callBack)
tid = create_a_thread_and_load_MyDll() # where I make the dll instance and all the work need into a thread
#into a someone function into the main thread
def callBack(): # a simple callback
reactor.callIntoThread(tid, funct, callBack)
Is this possible with twisted? (Does I'm dreaming?)
P.s. If I try with the wx "reactor" (so wx.MainLoop), I see no problem! In that case, I instance the dll into the class __init__ (inherit from wx.Frame) and into a someone event (i.e. Button click) I call a dll funct and all work without crash...
Thanks, Michele
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
G�bor Bern�th already answered your question, but I'd like to answer as specifically as possible for future readers who may discover this message. On 13 Jun, 06:21 pm, michele.petrazzo@unipex.it wrote:
reactor.call_in_main_thrad(callBack)
This is spelled: reactor.callFromThread(callBack)
reactor.callIntoThread(tid, funct, callBack)
This is spelled: from twisted.internet.threads import deferToThread deferToThread(funct).addCallback(callBack)
Is this possible with twisted? (Does I'm dreaming?)
This, and many other fun features of the reactor, can be discovered by reading the twisted.internet interface documentation, available here: http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.h... You can read more specifically about threads here: http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.I... I hope this proves useful to you!
participants (3)
-
glyph@divmod.com -
Gábor Bernáth -
Michele Petrazzo - Unipex srl