[Twisted-Python] 3 questions about async, threading and dynamic protocol selection

甜瓜 littlesweetmelon at gmail.com
Wed Apr 4 06:11:04 EDT 2007

Q1: How to determine a function is 'blocking action' or not?
Any function needs CPU times. Indeed, a computational-intensive
function is blocking action. But how small/fast function can be
classified as non-blocking? Twist requires all user functions to be
non-blocked. If reactor calls a blocking function, what will happen?
In my mind, reactor maintains a command queue internally (just like
windows message queue). The blocking function only postpones the
execution of other queued functions, but it does not break the logic
of the program. Is that right?

Q2: Today when I go through the twist document, I am confused about
the threading problem in reactor.
What is the different between 'reactor.callFromThread' and a plain
call in reactor loop?
def callFromThread(f):
            self.threadCallQueue.append((f, args, kw))
It seems equivalent to reactor.callLater(0, f...).
What is the real circumstance for calling callFromThread?
Further more, how to determine a function is thread-safe in python?
Why the twist doc says: "writing data to a transport from a protocol
is not thread-safe."?

Q3: In my application, I need a facility to dynamically select a
protocol to communicate with the server.
Eg: When connected, the server sent a string to client to indicate the
version of protocol it used. Then, the client can load the proper
protocol. But I don't know how to implement this. A 'Factory' can only
create one kind of 'Protocol', and it seems the instances of
'Protocol' cannot share the connection (Transport object) to a server.
Could you give me some clues?

Thank you.


More information about the Twisted-Python mailing list