가능한 구성 변경을 위해 데이터베이스를 폴링해야하는 응용 프로그램이 있습니다. 응용 프로그램은 Twisted를 사용하는 간단한 xmlrpc 서버입니다. Twisted의 LoopingCall을 사용하여 폴링을 실험했지만 LoopingCall이 주 스레드에서 실행되기 때문에 db에 대한 호출이 차단됩니다. 그래서 어떤 이유로 DB 호출이 느린 경우 xmlrpc 서버에 대한 요청을 기다려야합니다. 그래서 LoopingCall을 하나의 스레드에서 실행하려고 시도했지만 실제로 작동시키지 못했습니다. 내 질문은 스레드에서 실행해야합니까? 그렇다면 어떻게?차단 기능이있는 꼬인 LoopingCall
from twisted.web import xmlrpc, server
from twisted.internet.threads import deferToThread
from twisted.internet import reactor, task
import platform
from time import sleep
r = reactor
class Agent(xmlrpc.XMLRPC):
self.routine()
xmlrpc.XMLRPC.__init__(self)
def xmlrpc_echo(self, x):
"""
Return arg as a simple test that the server is running
"""
return x
def register(self):
"""
Register Agent with db and pick up config
"""
sleep(3) # simulate slow db call
print 'registered with db'
def routine(self):
looping_register = task.LoopingCall(self.register)
looping_register.start(7.0, True)
if __name__ == '__main__':
r.listenTCP(7081, server.Site(Agent()))
print 'Agent is running on "%s"' % platform.node()
r.run()
을 당신은 안 이것을 지원하는 것으로 문서화되어 있지 않다면 다른 스레드에서 Twisted API를 실행하십시오. 거의 아무 트위스트 API도 이것을 지원하지 않습니다. 당신이 이런 식으로 사용하려고하면, 디버깅하기가 어렵습니다. –
그래, 그게 어려운 길을 배우고있어. 좋은 조언을 주셔서 감사합니다 – MFB