Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-blocking server in Twisted

Tags:

python

twisted

I am building an application that needs to run a TCP server on a thread other than the main. When trying to run the following code:

reactor.listenTCP(ServerConfiguration.tcpport, TcpCommandFactory())
reactor.run()

I get the following error

exceptions.ValueError: signal only works in main thread

Can I run the twisted servers on threads other than the main one?

like image 374
Eden Avatar asked Oct 16 '12 15:10

Eden


1 Answers

Twisted can run in any thread - but only one thread at a time. If you want to run in the non-main thread, simply do reactor.run(installSignalHandlers=False). However, you cannot use a reactor on the non-main thread to spawn subprocesses, because their termination will never be detected. (This is a limitation of UNIX, really, not of Twisted.)

like image 66
Glyph Avatar answered Sep 24 '22 09:09

Glyph