[Twisted-Python] thread in reactor problem

Hi all, I run a reactor in my thread, so I will get many connection by twisted, and I run another thread for write back data to any connection. But I've found the data is in buffer, not sent back immediately. I traced the twisted, found it blocked in class SelectReactor.doSelect():
while 1: try: r, w, ignored = _select(reads.keys(), writes.keys(), [], timeout) break ... ... #do sth
when the reactor is bloce by _select(), then my another thread modified the *writes* dictionary, but _select could not know if writes.keys() is changed, so when the thread want to write something, reactor won't do response. so, the problem is above.
Is there any idea for this? thanks~

On 8/7/07, Yan Zhu nayuhz@gmail.com wrote:
Hi all, I run a reactor in my thread, so I will get many connection by twisted, and I run another thread for write back data to any connection.
You don't need to have multiple threads to communicate with multiple connections!
But I've found the data is in buffer, not sent back immediately.
This is because you're calling Twisted functions from non-reactor threads. Twisted doesn't support this. The *only* threadsafe function to call is reactor.callFromThread. You pass it a function to run in the reactor thread.
If all you need is concurrent asynchronous I/O, you don't need threads. Twisted is an *asynchronous* networking framework. You can do all of your communication efficiently in one thread.

Thanks you. I've resolved it! ;)
On 8/8/07, Christopher Armstrong radix@twistedmatrix.com wrote:
On 8/7/07, Yan Zhu nayuhz@gmail.com wrote:
Hi all, I run a reactor in my thread, so I will get many connection by twisted, and I run another thread for write back data to any connection.
You don't need to have multiple threads to communicate with multiple connections!
But I've found the data is in buffer, not sent back immediately.
This is because you're calling Twisted functions from non-reactor threads. Twisted doesn't support this. The *only* threadsafe function to call is reactor.callFromThread. You pass it a function to run in the reactor thread.
If all you need is concurrent asynchronous I/O, you don't need threads. Twisted is an *asynchronous* networking framework. You can do all of your communication efficiently in one thread.
-- Christopher Armstrong International Man of Twistery http://radix.twistedmatrix.com/ http://twistedmatrix.com/ http://canonical.com/
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (2)
-
Christopher Armstrong
-
Yan Zhu