[Twisted-Python] hang when using twisted

gx1986 at mail.ustc.edu.cn gx1986 at mail.ustc.edu.cn
Fri Sep 18 09:48:36 MDT 2009



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! 




More information about the Twisted-Python mailing list