python信号 - 在单独的线程中执行回调

时间:2015-01-17 18:49:47

标签: multithreading python-2.7 signals scrapy twisted

某些背景......

我有一个线程让我的Rabbit消费者班RabbitClient说。

当客户端使用来自队列的消息时,它会将其提供给CrawlWorker - 一个管理蜘蛛生命周期的对象,并捕获已删除的项目。

要发布回队列,需要连接"发布"回调CrawlWorker的{​​{1}}信号,它就可以了。

但是,此回调在主线程上执行 - 而不是在持有Rabbit客户端的线程上执行。有没有办法确保在拥有Rabbit客户端的线程上调用回调?

这是一个非常简单的例子 - 我认为我已经将问题归结为其本质:

item_scraped

如果您运行此命令,则会收到以下消息:

import threading
import logging

from scrapy.signalmanager import SignalManager
from twisted.internet import reactor

logging.basicConfig(level=logging.INFO,
                    format='(%(threadName)-10s) %(message)s')

SIGNAL = object()


def func():
    message = 'Message from {0}!'.format(threading.currentThread().name)

    signals.send_catch_log(SIGNAL, message=message)


class ReceiverThread(threading.Thread):

    def __init__(self, signals):
        self.signals = signals
        self.signals.connect(self.receive, signal=SIGNAL)

        super(ReceiverThread, self).__init__(group=None, target=None,
                                             name='ReceiverThread', verbose=False)

    def receive(self, message):
        logging.info('Received message: {0}'.format(message))

    def run(self):
        # func()
        pass


if __name__ == '__main__':
    signals = SignalManager()

    thread = ReceiverThread(signals)
    thread.start()

    func()

    reactor.run()

(MainThread) Received message: Message from MainThread! 中对func()来电进行评论,并在run()中评论func()来电,将会给出:

__main__

我没有在线程编程方面经验丰富,所以大多数读这篇文章的人都可能会有这些结果,但有没有办法获得:

(ReceiverThread) Received message: Message from ReceiverThread!

0 个答案:

没有答案