Twisted reactor 'signal only works in main thread' error?

Richard richardd at hmgcc.gov.uk
Thu Jun 5 05:52:51 EDT 2003


Hi,

I am using execfile inside a Thread derived class to run another Python
script. The source for this script is below. The problem is, when I try to
run it I get the following error message:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python22\lib\threading.py", line 408, in __bootstrap
    self.run()
  File "T:\code\python\TestServer.py", line 25, in run
    execfile(self.m_File, self.m_ScriptVars)
  File "URFParser.py", line 37, in ?
    m_URFServer.start()
  File "URFParser.py", line 32, in start
    reactor.run()
  File "C:\Python22\Lib\site-packages\twisted\internet\default.py", line
122, in
 run
    self.startRunning(installSignalHandlers=installSignalHandlers)
  File "C:\Python22\Lib\site-packages\twisted\internet\default.py", line
116, in
 startRunning
    self._handleSignals()
  File "C:\Python22\Lib\site-packages\twisted\internet\default.py", line 88,
in
_handleSignals
    signal.signal(signal.SIGINT, self.sigInt)
ValueError: signal only works in main thread

####

I've had a look at the Twisted docs but am only very new to Twisted and so
can't really figure out how to fix this, am I right in thinking that a
reactor can only operate inside the main thread of a program due to some
signalling it uses? If so, is there any way around it?

The source for TestServer.py is here:

####

from twisted.internet.protocol import Protocol, ServerFactory
from twisted.internet import reactor

class ConnectionEndpoint(Protocol):

 def connectionMade(self):
  #Executed in a new thread for every incoming connection
  self.transport.write(self.factory.m_TestString)

 def dataReceived(self, data):
  print data

class ConnectionEndpointFactory(ServerFactory):
 protocol = ConnectionEndpoint
 m_FrameQueue = None
 m_TestString = "Test"

 def setQ(self, q):
  self.m_FrameQueue = q


class URFServer:
 m_Address = []
 m_Factory = ConnectionEndpointFactory()

 def __init__(self, q, address):
  self.m_Factory.setQ(q)
  self.m_Address = address

 def start(self):
  reactor.listenTCP(int(self.m_Address[1]), self.m_Factory)
  reactor.run()


#main
m_URFServer = URFServer(frameQueue, address)
m_URFServer.start()


#####

TIA

Richard







More information about the Python-list mailing list