本文主要是介绍pika消费者退出,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
pika消费者退出问题
https://stackoverflow.com/questions/32220057/interrupt-thread-with-start-consuming-method-of-pika
import threading
import pikaclass WorkerThread(threading.Thread):def __init__(self):super(WorkerThread, self).__init__()self._is_interrupted = Falsedef stop(self):self._is_interrupted = Truedef run(self):connection = pika.BlockingConnection(pika.ConnectionParameters())channel = connection.channel()channel.queue_declare("queue")for message in channel.consume("queue", inactivity_timeout=1):if self._is_interrupted:breakif not message:continuemethod, properties, body = messageprint(body)def main():thread = WorkerThread()thread.start()# some main thread activity ...thread.stop()thread.join()if __name__ == "__main__":main()
这篇关于pika消费者退出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!