[Twisted-Python] hang when using twisted
I met some problem when writing twisted. The code is 1 #! /usr/bin/python 2 # fileName remoteCopyFile 3 from twisted.internet import protocol , reactor 4 import os 5 import updateCurrentIPList 6 import time 7 8 PORT = 7777 9 10 class TSClientProtocol(protocol.Protocol): 11 def sendData(self): 12 path = '/usr/local/'+self.factory.filename 13 if os.path.isfile(path): 14 f = open(path,'rb') 15 data = f.read() 16 f.close() 17 self.transport.write("filename="+self.factory.filename+'=') 18 self.transport.write(data) 19 self.transport.write('EOF') 20 else: 21 self.transport.loseConnection() 22 23 def connectionMade(self): 24 self.sendData() 25 26 27 def dataReceived(self,data): 28 if data == 'EOF': 29 print 'finished' 30 self.transport.loseConnection() 31 32 33 class TSClientFactory(protocol.ClientFactory): 34 def __init__(self,filename): 35 self.filename = filename 36 protocol = TSClientProtocol 37 clientConnectionLost = clientConnectionFailed = lambda self,connector,reason:reactor.stop() 38 39 def remoteCopyFile(HOST,filename = '4'): 40 reactor.connectTCP(HOST,PORT,TSClientFactory(filename)) 41 reactor.run(installSignalHandlers=0) 42 updateCurrentIPList.updateCurrentIPList(HOST) 43 reactor.connectTCP(HOST,PORT,TSClientFactory('currentIP')) 44 reactor.run(installSignalHandlers=0) 45 46 47 if __name__ == '__main__': 48 remoteCopyFile('127.0.0.1') The program print two "finished",then hang. If i delete the line 40 and 41 or delete the line 43 and 44 ,the program become good. whether can't we use two "reactor.run()" in one function? why? Thank you!
On 03:48 pm, gx1986@mail.ustc.edu.cn wrote:
I met some problem when writing twisted.
The code is
[snip] 38 39 def remoteCopyFile(HOST,filename = '4'): 40 reactor.connectTCP(HOST,PORT,TSClientFactory(filename)) 41 reactor.run(installSignalHandlers=0) 42 updateCurrentIPList.updateCurrentIPList(HOST) 43 reactor.connectTCP(HOST,PORT,TSClientFactory('currentIP')) 44 reactor.run(installSignalHandlers=0) 45 [snip]
The program print two "finished",then hang. If i delete the line 40 and 41 or delete the line 43 and 44 ,the program become good. whether can't we use two "reactor.run()" in one function? why? Thank you!
Yes, you cannot call reactor.run() twice (anywhere in the same program - whether within one function or otherwise). This is an minor implementation limitation which could fairly easily be lifted if anyone really wanted to. However, there's not a lot of reason to bother. Instead of stopping the reactor after your first task and then starting it for your second task, just start your second task when the first one is done - don't stop the reactor and then start it again. When you're done with everything and ready to exit, that's when you should stop the reactor. Jean-Paul
participants (2)
-
exarkun@twistedmatrix.com
-
gx1986@mail.ustc.edu.cn