[Twisted-Python] reactor and thread
Gábor Bernáth
gabor.bernath at gmail.com
Wed Jun 13 15:01:37 EDT 2007
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#deferToThread
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 at 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 at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
>
More information about the Twisted-Python
mailing list