Scrapy:非阻塞暂停

JRa*_*zor 19 python scrapy scrapy-spider

我有个问题.我需要停止一段时间的函数执行,但不要停止整个解析的实现.也就是说,我需要一个非阻塞暂停.

它看起来像:

class ScrapySpider(Spider):
    name = 'live_function'

    def start_requests(self):
        yield Request('some url', callback=self.non_stop_function)

    def non_stop_function(self, response):
        for url in ['url1', 'url2', 'url3', 'more urls']:
            yield Request(url, callback=self.second_parse_function)

        # Here I need some function for sleep only this function like time.sleep(10)

        yield Request('some url', callback=self.non_stop_function)  # Call itself

    def second_parse_function(self, response):
        pass
Run Code Online (Sandbox Code Playgroud)

函数non_stop_function需要暂停一段时间,但不应该阻止输出的其余部分.

如果我插入time.sleep()- 它将停止整个解析器,但我不需要它.是否可以使用twisted其他功能停止一个功能?

原因:我需要创建一个非阻塞函数,每n秒解析一次网站页面.在那里,她将获得网址并填写10秒钟.已获取的URL将继续有效,但主要功能需要休眠.

更新:

感谢TkTechviach.一个答案帮助我理解了如何进行挂起Request,第二个是如何激活它.两个答案相互补充,我为Scrapy做了一个非常好的非阻塞暂停:

def call_after_pause(self, response):
    d = Deferred()
    reactor.callLater(10.0, d.callback, Request(
        'https://example.com/',
        callback=self.non_stop_function,
        dont_filter=True))
    return d
Run Code Online (Sandbox Code Playgroud)

并根据我的要求使用此功能:

yield Request('https://example.com/', callback=self.call_after_pause, dont_filter=True)
Run Code Online (Sandbox Code Playgroud)

Via*_*kyi 6

Request对象有callback参数,尽量使用那个参数.我的意思是,创造一个Deferred包装self.second_parse_functionpause.

这是我的脏和未经测试的例子,标记了更改的行.

class ScrapySpider(Spider):
    name = 'live_function'

    def start_requests(self):
        yield Request('some url', callback=self.non_stop_function)

    def non_stop_function(self, response):

        parse_and_pause = Deferred()  # changed
        parse_and_pause.addCallback(self.second_parse_function) # changed
        parse_and_pause.addCallback(pause, seconds=10)  # changed

        for url in ['url1', 'url2', 'url3', 'more urls']:
            yield Request(url, callback=parse_and_pause)  # changed

        yield Request('some url', callback=self.non_stop_function)  # Call itself

    def second_parse_function(self, response):
        pass
Run Code Online (Sandbox Code Playgroud)

如果该方法适用于您,则可以创建一个Deferred根据规则构造对象的函数.它可以通过以下方式实现:

def get_perform_and_pause_deferred(seconds, fn, *args, **kwargs):
    d = Deferred()
    d.addCallback(fn, *args, **kwargs)
    d.addCallback(pause, seconds=seconds)
    return d
Run Code Online (Sandbox Code Playgroud)

以下是可能的用法:

class ScrapySpider(Spider):
    name = 'live_function'

    def start_requests(self):
        yield Request('some url', callback=self.non_stop_function)

    def non_stop_function(self, response):
        for url in ['url1', 'url2', 'url3', 'more urls']:
            # changed
            yield Request(url, callback=get_perform_and_pause_deferred(10, self.second_parse_function))

        yield Request('some url', callback=self.non_stop_function)  # Call itself

    def second_parse_function(self, response):
        pass
Run Code Online (Sandbox Code Playgroud)

  • 我不明白暂停功能是什么以及它从何而来。 (2认同)

TkT*_*ech 5

如果您尝试将其用于速率限制,则可能只想使用DOWNLOAD_DELAY.

Scrapy只是Twisted上的一个框架.在大多数情况下,您可以像对待任何其他扭曲的应用程序一样对待它.而不是调用sleep,只需返回下一个请求,然后告诉twisted等待一下.例如:

from twisted.internet import reactor, defer

def non_stop_function(self, response)
    d = defer.Deferred()
    reactor.callLater(10.0, d.callback, Request(
        'some url',
        callback=self.non_stop_function
    ))
    return d
Run Code Online (Sandbox Code Playgroud)