SignalManager in scrapy

Scrapy中的信号量通过SignalManager类实现 #

class SignalManager(object):

def __init__(self, sender=dispatcher.Anonymous):
self.sender = sender

def connect(self, receiver, signal, **kwargs):
"""
Connect a receiver function to a signal.

The signal can be any object, although Scrapy comes with some
predefined signals that are documented in the :ref: `topics-signals`
section.

:param receiver: the function to be connected
:type receiver: callable

:param signal: the signal to connect to
:type signal: object
"""

kwargs.setdefault('sender', self.sender)
return dispatcher.connect(receiver, signal, **kwargs)

SignalManager通过PyDispatcher来实现

from pydispatch import dispatcher
PyDispatcher官方文档介绍,PyDispatcher是一个在多个上下文中的多消费者-多生产者-信号处理机制。它主要为了改善包含多个application项目的开发。

receiver-register(registration)-signal(message)-sender #

首先理解 receiver-register(registration)-signal(message)-sender
receiver和sender即接受和发送方,接受需要一个注册的过程,定义接受的来源和类型。

features #

摘自官方文档

# set up receiver
from pydispatch import dispatcher
SIGNAL = 'my-first-signal'

def handle_event( sender ):
"""Simple event handler"""
print 'Signal was sent by', sender
dispatcher.connect( handle_event, signal=SIGNAL, sender=dispatcher.Any )

# set up sender
first_sender = object()
# can be any callable object
second_sender = {}
def main( ):
dispatcher.send( signal=SIGNAL, sender=first_sender )
dispatcher.send( signal=SIGNAL, sender=second_sender )

End.💖

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated! If any questions, plz contact me!

Published