[Twisted-Python] How to accept connections faster
Antoine Pitrou
solipsis at pitrou.net
Wed Mar 16 03:44:04 MST 2005
> def connectionMade(self):
>
> print "Got a connection from on " + time.asctime()
> self.transport.write("Welcome. You are client number : ")
> print "Looping..."
> for i in range(10000000):
> pass
Why are you doing that ?
An event loop-based server like Twisted is not multithreaded. It means
during the handling of any event (e.g. connectionMade), other events
will not be processed, they will just be queued (by the OS usually).
When you return from the event handler, the Twisted event loop will
resume and process other events.
If you really have heavy calculations and want to keep good latencies,
you have two solutions:
- either slice your calculations in small pieces and call them one after
the other with reactor.callLater(), which ensures that you let other
events slip by
- or defer the calculations to a helper thread or process, with which
you will have to synchronize in the traditional way
(reactor.callFromThread() and reactor.callInThread() may help... I don't
know)
Good luck
Antoine.
More information about the Twisted-Python
mailing list