我想检查消费者/工人是否存在消费我即将发送的消息。
如果没有任何Worker,我会启动一些工人(消费者和发布者都在一台机器上),然后开始发布Messages。
如果有类似的功能connection.check_if_has_consumers
,我会像这样实现它 -
import pika
import workers
# code for publishing to worker queue
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
# if there are no consumers running (would be nice to have such a function)
if not connection.check_if_has_consumers(queue="worker_queue", exchange=""):
# start the workers in other processes, using python's `multiprocessing`
workers.start_workers()
# now, publish with no fear of your queues getting filled up
channel.queue_declare(queue="worker_queue", auto_delete=False, durable=True)
channel.basic_publish(exchange="", routing_key="worker_queue", body="rockin",
properties=pika.BasicProperties(delivery_mode=2))
connection.close()
check_if_has_consumers
但是我在pika中找不到任何具有功能的功能。
有没有办法使用pika来完成这个?或者,直接与兔子交谈?
我不完全确定,但我真的认为RabbitMQ会知道订阅不同队列的消费者数量,因为它确实向他们发送消息并接受确认
我 3 小时前刚开始使用RabbitMQ ......欢迎任何帮助......
这是我写的workers.py代码,如果有帮助的话....
import multiprocessing
import pika
def start_workers(num=3):
"""start workers as non-daemon processes"""
for i in xrange(num):
process = WorkerProcess()
process.start()
class WorkerProcess(multiprocessing.Process):
"""
worker process that waits infinitly for task msgs and calls
the `callback` whenever it gets a msg
"""
def __init__(self):
multiprocessing.Process.__init__(self)
self.stop_working = multiprocessing.Event()
def run(self):
"""
worker method, open a channel through a pika connection and
start consuming
"""
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost')
)
channel = connection.channel()
channel.queue_declare(queue='worker_queue', auto_delete=False,
durable=True)
# don't give work to one worker guy until he's finished
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback, queue='worker_queue')
# do what `channel.start_consuming()` does but with stopping signal
while len(channel._consumers) and not self.stop_working.is_set():
channel.transport.connection.process_data_events()
channel.stop_consuming()
connection.close()
return 0
def signal_exit(self):
"""exit when finished with current loop"""
self.stop_working.set()
def exit(self):
"""exit worker, blocks until worker is finished and dead"""
self.signal_exit()
while self.is_alive(): # checking `is_alive()` on zombies kills them
time.sleep(1)
def kill(self):
"""kill now! should not use this, might create problems"""
self.terminate()
self.join()
def callback(channel, method, properties, body):
"""pika basic consume callback"""
print 'GOT:', body
# do some heavy lifting here
result = save_to_database(body)
print 'DONE:', result
channel.basic_ack(delivery_tag=method.delivery_tag)
编辑:
我必须继续前进,所以这是我要采取的解决方法,除非出现更好的方法,
所以,RabbitMQ有这些HTTP 管理 api,它们在你打开管理插件后工作,并且在 HTTP api 页面中间有
/api/connections - 所有打开的连接的列表。
/api/connections/name - 单个连接。删除它将关闭连接。
所以,如果我通过不同的连接名称/用户连接我的工人和我的产品,我将能够检查工人连接是否打开......(工人死亡时可能会出现问题......)
将等待更好的解决方案...
编辑:
刚刚在 rabbitmq 文档中找到了这个,但这在 python 中会很麻烦:
shobhit@oracle:~$ sudo rabbitmqctl -p vhostname list_queues name consumers
Listing queues ...
worker_queue 0
...done.
所以我可以做类似的事情,
subprocess.call("echo password|sudo -S rabbitmqctl -p vhostname list_queues name consumers | grep 'worker_queue'")
hacky ......仍然希望 pika 有一些 python 函数来做到这一点......
谢谢,