scrapy / scrapy

Scrapy, a fast high-level web crawling & scraping framework for Python.
https://scrapy.org
BSD 3-Clause "New" or "Revised" License
53.16k stars 10.56k forks source link

Allow passing parameters to signal receiver #6481

Closed genismoreno closed 1 month ago

genismoreno commented 1 month ago

Summary

Allow passing parameters to a signal receiver (when self is not available)

I.e.

crawler.signals.connect(receiver=cls.engine_stopped, signal=signals.engine_stopped, cb_kwargs={"lazy": True})

@classmethod
def engine_stopped(cls, lazy: bool) -> None:
    ...

Motivation

Pass of parameters to the receiver method would allow more dependent logic/behavior in it

Gallaecio commented 1 month ago

You could always create a callable object instead, e.g.

class EngineStopped:
     def __init__(self, lazy):
        self.lazy = lazy
    def __call__(self, *args, **kwargs):
        ...  # Use self.lazy

crawler.signals.connect(receiver=EngineStopped(lazy=True), signal=signals.engine_stopped)
vishvjeet-thakur commented 1 month ago

is this issue resolved or can I work on it? @Gallaecio @genismoreno @

Gallaecio commented 1 month ago

@genismoreno Feel free to reopen if you find a scenario where the solution above is not good enough.

genismoreno commented 1 month ago

I think that with the solution @Gallaecio proposed should be enough. Thank you!!