我有一個以Python編寫的控制檯類型的應用程序,用於觸發遠程連接,實際連接永遠不會被觸發。Python扭曲連接永不觸發
這裏是應用程序的削減版本:
import sys
import time
from twisted.internet import reactor, threads, protocol
from twisted.internet.endpoints import TCP4ClientEndpoint
from twisted.protocols.basic import LineReceiver
host = 'localhost'
port = 8080
class InterfaceFactory(protocol.Factory):
def buildProtocol(self, addr):
return protocol()
def ConnectToMaster():
deferItem = endpoint.connect(factory)
deferItem.addErrback(ConnectFailed)
print('Connecting to master')
def MainProcessLoop():
done = False
while True:
if done == False:
print 'Triggered connect'
reactor.callLater(0.1, ConnectToMaster)
done = True
time.sleep(1)
def ConnectFailed(self, reason):
print('Failed to connect... : reason of {0}'.format(reason))
endpoint = TCP4ClientEndpoint(reactor, host, port)
protocol = LineReceiver()
factory = InterfaceFactory()
reactor.callFromThread(MainProcessLoop)
reactor.run()
輸出是:
$ python testCode.py
Triggered connect
我真的很困惑,這應該被扭曲101,...... MainProcessLoop處於線程,但是它是一個由Twisted管理的線程,加上reactor.run()不是任何形式的線程。
MainProcessLoop是否處於循環狀態並可以阻止?實際上,在那裏有一個get_input(),但是爲了代碼片段而將其切掉。
爲什麼要將reactor.callLater(0.1,ConnectToMaster)更改爲reactor.callFromThread(reactor.callLater,0.1,ConnectToMaster)? –
因爲你試圖在一個線程中執行'reactor'依賴的東西,'reactor'不是線程安全的。 'callFromThread'將在「main」線程中運行函數,在你希望連接到服務器的情況下,這是你想要的。 –