[Twisted-Python] Abouth threads in twisted.

Peter Cai newptcai at gmail.com
Tue Aug 19 06:08:19 MDT 2008


Hi All,

QUESTION 1 : callFromThread
~~~~~~~~~~~~~~~~~~~~~~~~~~~

I found this code snippet in twisted manual book.
But I don't quite get it.

What does "run 'notThreadSafe(3)' in the event loop" means?

Does the reactor promise that all those "notThreadSafe" functions
be called one by one so they won't interfere each other?

#======================================

from twisted.internet import reactor

def notThreadSafe(x):
    """do something that isn't thread-safe"""
    # ...

def threadSafeScheduler():
    """Run in thread-safe manner."""

reactor.callFromThread(notThreadSafe, 3) # will run 'notThreadSafe(3)' in
the event loop

#======================================


QUESTION 2 : Integrated with a threaded 3rd party library.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I have a C library which using a work threaded pool model.  It's interface
is like this.

    void runWorkAsyn(int args, void pcallbackfunc(int));

When runWorkAsyn is called, it dispatch work to a thread in its
own thread pool and call 'pcallbackfunc' in that thread when it's done.

I really got confused by how use this lib from twisted in a thread safe way.
If I use a python callback function as pcallbackfunc and touch the reactor
from it,
it might damage the event look as most twisted APIs are not thread safe.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20080819/f7fc712f/attachment.html>


More information about the Twisted-Python mailing list