我不知道如何在twisted.internet.loopingCall()
twisted.internet.ClientFactory
之类的内容
我需要编写python脚本,扫描目录以查找带有电话号码的传入文件,读取它们,并使用使用yaypm
库的YATE twisted
python模块进行调用。
client_factory = yaypm.TCPDispatcherFactory(start_client)
reactor.connectTCP(host, port, client_factory)
reactor.run()
从yaypm.TCPDispatcherFactory
和twisted.internet.ClientFactory
派生的start_client
是成功连接后将执行的函数。
如果start_client
仅进行演示通话:
def start_client(client_yate):
d = dialer(client_yate)
d.call(caller, target)
一切都好。
(dialer
是实现yaypm.flow
逻辑的对象,完整描述位于http://docs.yate.ro/wiki/YAYPM:Bridge_and_then_unbridge)
我需要在start_client
d = dialer(client_yate)
files = os.listdir(input_directory)
for filename in files:
<read caller and target numbers from file>
d.call(caller, target)
time.sleep(interval)
我知道在主线程中使用sleep函数会导致死锁。 我该如何实现上面的算法?
答案 0 :(得分:1)
sleep()
装饰器, twisted.internet.task.deferLater
就像inlineCallbacks
调用一样。以下是使用ClientFactory
:
from twisted.internet import reactor, task
from twisted.internet.defer import inlineCallbacks
from twisted.internet.protocol import Protocol, ClientFactory
class DoNothing(Protocol):
def __init__(self, connection_callback):
self.connection_callback = connection_callback
def connectionMade(self):
self.connection_callback()
return
class ConnectionClientFactory(ClientFactory):
def __init__(self, connection_callback):
self.connection_callback = connection_callback
def buildProtocol(self, addr):
return DoNothing(self.connection_callback)
def sleep(delay):
# Returns a deferred that calls do-nothing function
# after `delay` seconds
return task.deferLater(reactor, delay, lambda: None)
@inlineCallbacks
def repeat_forever(message):
while True:
print(message)
yield sleep(1)
if __name__ == '__main__':
repeat_forever('running')
factory = ConnectionClientFactory(lambda: repeat_forever('connected'))
reactor.connectTCP('example.com', 80, factory)
reactor.run()
上面的代码基本上就是你的库对你传入的回调所做的。正如你所看到的,对repeat_forever('running')
的调用与客户端连接后调用的调用同时进行。