我试图让一个基于扭曲的鼠兔(RabbitMQ)消费者加入服务。以下是我目前所处的相关内容:
worker.py
import pika
from pika.adapters import twisted_connection
from twisted.internet import defer, reactor, protocol, task
@defer.inlineCallbacks
def run(connection):
channel = yield connection.channel()
yield channel.queue_declare(
queue='event_queue',
durable=True, auto_delete=False, exclusive=False
)
queue_object, consumer_tag = yield channel.basic_consume(
queue='event_queue', no_ack=False
)
l = task.LoopingCall(process, queue_object, connection)
l.start(0.01)
@defer.inlineCallbacks
def process(queue_object, connection):
ch, method, properties, body = yield queue_object.get()
if body:
print "Do stuff here"
yield ch.basic_ack(delivery_tag=method.delivery_tag)
def start():
parameters = pika.ConnectionParameters()
cc = protocol.ClientCreator(
reactor, twisted_connection.TwistedProtocolConnection, parameters
)
d = cc.connectTCP(MQ_SERVER, MQ_SERVER_PORT)
d.addCallback(lambda x: x.ready)
d.addCallback(run)
使用以下脚本运行时,此工作正常工作:
runserver.py
import worker
from twisted.internet import reactor
if __name__ == '__main__':
worker.start()
reactor.run()
我无法弄清楚如何在.tac文件中使用其他服务。
services.tac
from twisted.application import service
application = service.Application("An application")
from other import get_service
other_service = get_service()
other_service.setServiceParent(application)
import worker
worker.start()
此tac文件在运行时无效。没有其他服务的相同代码似乎可以工作,但是当启用其他服务时它似乎不起作用。相反,它会返回延时的超时时间。
有没有办法可以将这个connectTCP转换为常规服务?
答案 0 :(得分:0)
您必须使用twistd:
运行.tac
文件
$ twistd -ny services.tac