[Dev] Twisted has a home on Chandler Wiki
Brian Kirsch
bkirsch at osafoundation.org
Tue Jun 1 10:08:42 PDT 2004
Thanks Anthony,
We are going to run Twisted in a thread and use the callFromThread API
as you suggested:
Here is my basic thread code:
class ReactorException(Exception):
def __init__(self, *args):
Exception.__init__(self, *args)
class ReactorThread(threading.Thread):
"""Run the Reactor in a Thread to prevent blocking the
Main Thread once reactor.run is called"""
def __init__(self):
threading.Thread.__init__(self)
self._reactorRunning = False
def run(self):
if self._reactorRunning:
raise ReactorException("Reactor Already Running")
self._reactorRunning = True
#call run passing a False flag indicating to the
#reactor not to install sig handlers since sig handlers
#only work on the main thread
reactor.run(False)
def callInReactor(self, callable, *args, **kw):
if self._reactorRunning:
reactor.callFromThread(callable, *args, **kw)
else:
callable(*args, **kw)
def isReactorRunning(self):
return self._reactorRunning
def startReactor(self):
if self._reactorRunning:
raise ReactorException("Reactor Already Running")
threading.Thread.start(self)
reactor.addSystemEventTrigger('after', 'shutdown',
self.__reactorShutDown)
def stopReactor(self):
"""may want a way to force thread to join if reactor does
not shutdown
properly"""
if not self._reactorRunning:
raise ReactorException("Reactor Not Running")
reactor.callFromThread(reactor.stop)
def addReactorEventTrigger(self, phase, eventType, callable):
if self._reactorRunning:
reactor.callFromThread(reactor.addSystemEventTrigger,
phase, eventType, callable)
else:
reactor.addSystemEventTrigger(phase, eventType,
callable)
def __reactorShuttingDown(self):
pass
def __reactorShutDown(self):
"""This method called when the reactor is stopped"""
self._reactorRunning = False
Brian Kirsch - Email Framework Engineer
Open Source Applications Foundation
543 Howard St. 5th Floor
San Francisco, CA 94105
(415) 946-3056
On May 31, 2004, at 6:56 PM, Anthony Baxter wrote:
> Andi Vajda wrote
>> With that in mind, my opinion, for what it's worth, is that we should
>> have the
>> following threading model:
>> - a UI thread
>> - a twisted thread
>> - a task manager thread pool to which tasks are assigned and
>> scheduled
>> All Twisted-based networking should be done via the twisted thread,
>> which
>> involves a fair amount of inter-thread communication, but that's what
>> the
>> Twisted model is all about isn't it ?
>
> Note that if you're using wx, you really do need a separate thread
> for the wx and twisted event loops. If you try to make the twisted
> event loop drive the wx event loop you'll find that menus and popup
> dialogs spawn a sub-event-loop, and twisted will block while that's
> running. If you try to make the wx event loop drive twisted (using
> a wxTimer) you run into the wxTimer's essential suckiness - it
> guarantees only 1-100 iterations per second (and on windows you're
> lucky to get 100.
>
> In twisted there's a 'callFromThread()' method that allows other
> threads to inject stuff into the twisted event loop.
>
> I know about this because we hit this problem with shtoom's wx UI.
> Check the shtoom svn trunk for example code.
>
>
>
>
> --
> Anthony Baxter <anthony at interlink.com.au>
> It's never too late to have a happy childhood.
> _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
>
> Open Source Applications Foundation "Dev" mailing list
> http://lists.osafoundation.org/mailman/listinfo/dev
More information about the Dev
mailing list