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`

: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)


from pydispatch import dispatcher

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

首先理解 receiver-register(registration)-signal(message)-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 )


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!