[Twisted-Python] Twisted, wxPython & PyPubSub

Gabriel Rossetti gabriel.rossetti at arimaz.com
Fri Sep 11 08:58:48 EDT 2009

exarkun at twistedmatrix.com wrote:
> On 01:47 pm, gabriel.rossetti at arimaz.com wrote:
>> exarkun at twistedmatrix.com wrote:
>>> On 9 Sep, 07:12 am, gabriel.rossetti at arimaz.com wrote:
>>>> exarkun at twistedmatrix.com wrote:
>>>>> On 02:21 pm, gabriel.rossetti at arimaz.com wrote:
>>>>>> Hello everyone,
>>>>>> I am using wx with twisted and pubsub (not the on in wx but the
>>>>>> independent one) to notify each one of what is going on. I was
>>>>>> wondering
>>>>>> if I should use reactor.callFromThread to call 
>>>>>> publisher.sendMessage
>>>>>> or
>>>>>> not? I haven't been doing that until now but I wondered if it was
>>>>>> better to.
>>>>> Use reactor.callFromThread if you have code running in a non-reactor
>>>>> thread and you want it to initiate some action in the reactor 
>>>>> thread.
>>>>> So, if publisher.sendMessage is using Twisted APIs or otherwise
>>>>> requires
>>>>> that it be run only in the reactor thread, and you need to use it 
>>>>> from
>>>>> a
>>>>> non-reactor thread, then use reactor.callFromThread.  Otherwise,
>>>>> don't.
>>>>> Jean-Paul
>>>> Would you consider the wxreactor like a non-reactor thread?
>>> I'm not sure what you mean.  The "reactor thread" is the thread that
>>> "reactor.run()" is called in.  Even if you're using wxreactor.
>>>> I haven't
>>>> checked out the code behind PyPubSub, but it's basically the
>>>> Observer-Pattern so when I call publisher.sendMessage(...) it 
>>>> iterates
>>>> the "subscribers" (observers) and calls the functions they associated
>>>> w/
>>>> the subscription. Those functions are both in the twisted code and 
>>>> the
>>>> wx code. The Twisted code sends messages nd the wx code brings- 
>>>> up/hides
>>>> dialogs or frames. In the wxdemo it does :
>>>> # look, we can use twisted calls!
>>>> reactor.callLater(2, self.twoSecondsPassed)
>>>> but doesn't use reactor.callFromThread, but unless I'm wrong
>>>> reactor.callLater does the same thing but deferred in time. I often 
>>>> see
>>>> reactor.callLater(0, myFunc) being used.
>>> The wx demo doesn't do anything with threads, so it doesn't need to 
>>> use
>>> callFromThread.  So, if you're only doing things like what the wx demo
>>> does, you probably don't need callFromThread either.
>>> callLater doesn't do the same thing as callFromThread.  There might be
>>> some vague similarities (really the only one is that neither one calls
>>> the function you pass it right away), but they're for very different
>>> things.
>>> Jean-Paul
>> Ok, well, from looking into the twisted source, it looks like the wx
>> main loop is run in a seperate thread
>> (http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.1.0/twisted/internet/wxreactor.py#L60).
> That's an implementation detail.  You've also misinterpreted it 
> slightly.  The wx main loop is run in the same thread reactor.run() is 
> called in.  The other thread is used for other things.
> Jean-Paul
Ok, thanks for the clarification, so if I understand correctly then I 
can make wx calls (like self.factory.myFrame.Show()) from twisted just 
like if it was 100% twisted, and make twisted calls (like 
self.txClient.transport.write()) from wx just like if it was 100% wx? 
Cool :-)


More information about the Twisted-Python mailing list