[Twisted-Python] Calling into Twisted from a Windows service control

Eric Mangold teratorn at twistedmatrix.com
Mon Feb 12 08:14:53 MST 2007


On Mon, 12 Feb 2007 02:11:06 -0600, Steve Freitas <sflist at ihonk.com> wrote:

> On Sun, 2007-02-11 at 00:35 -0600, Eric Mangold wrote:
>> On Thu, 08 Feb 2007 12:35:03 -0600, Steve Freitas <sflist at ihonk.com>  
>> wrote:
>>
>> > Hi,
>> >
>> > Itamar posted earlier in this list how to run Twisted as a service:
>> >
>> >  
>> http://twistedmatrix.com/pipermail/twisted-python/2003-October/006081.html
>> >
>> > Now, I want to trap some Windows events and make some calls into my
>> > Twisted app (calling disconnect() on a pb.PBClientFactory instance is
>> > one example). I'll be setting up the trapping in the service code  
>> above,
>> > though, so from looking at the howtos, it looks like the right thing  
>> for
>> > me to do when I get some event is:
>> >
>> > reactor.callFromThread(factory.disconnect)
>> >
>> > The docs state, though, that the callable must be thread safe. Does  
>> that
>> > mean I need to use threadable.synchronize() to put a lock around
>> > factory.disconnect? Or whatever else I'm calling into?
>> >
>> > Steve
>>
>> I guess these are the docs you're refering to:
>>
>> http://twistedmatrix.com/projects/core/documentation/howto/threading.html
>>
>> Actually, they don't say that the callable must be thread-safe. Indeed,
>> the only reason you need to use callFromThread is if the callable *is  
>> not*
>> thread-safe. What callFromThread does is schedule the call to take place
>> within the main thread at the convenience of the reactor.
>
> Thanks for responding, Eric. After a couple of days I started to worry
> about the quality of my question.
>
> This is where I saw that note I mentioned above:
>
> http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.IReactorThreads.html
>
> That's part of what I found a little confusing... If I understand them
> correctly, these docs say the callable must be threadsafe, but if it is,
> I should be able to call it willy nilly.
>
> Steve

Wow, that doc string is quite misleading. Good catch :)

Please file a bug and assign to itamar since he has his name in that file  
:)

It's true that since you're writing a multi-threaded app the callable  
*may* need to be thread-safe. But in so far as callFromThread is concerned  
it doesn't need to be.

-- 
Eric Mangold
Twisted/Win32 Co-Maintainer




More information about the Twisted-Python mailing list