本文整理匯總了Python中scrapy.core.scraper.Scraper.is_idle方法的典型用法代碼示例。如果您正苦於以下問題:Python Scraper.is_idle方法的具體用法?Python Scraper.is_idle怎麽用?Python Scraper.is_idle使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scrapy.core.scraper.Scraper
的用法示例。
在下文中一共展示了Scraper.is_idle方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ExecutionEngine
# 需要導入模塊: from scrapy.core.scraper import Scraper [as 別名]
# 或者: from scrapy.core.scraper.Scraper import is_idle [as 別名]
class ExecutionEngine(object):
def __init__(self):
self.configured = False
self.closing = {} # dict (spider -> reason) of spiders being closed
self.running = False
self.killed = False
self.paused = False
self._next_request_calls = {}
self._crawled_logline = load_object(settings['LOG_FORMATTER_CRAWLED'])
def configure(self, spider_closed_callback):
"""
Configure execution engine with the given scheduling policy and downloader.
"""
self.scheduler = load_object(settings['SCHEDULER'])()
self.downloader = Downloader()
self.scraper = Scraper(self)
self.configured = True
self._spider_closed_callback = spider_closed_callback
def start(self):
"""Start the execution engine"""
assert not self.running, "Engine already running"
self.start_time = time()
send_catch_log(signal=signals.engine_started, sender=self.__class__)
self.running = True
def stop(self):
"""Stop the execution engine gracefully"""
assert self.running, "Engine not running"
self.running = False
dfd = self._close_all_spiders()
return dfd.addBoth(lambda _: self._finish_stopping_engine())
def kill(self):
"""Forces shutdown without waiting for pending transfers to finish.
stop() must have been called first
"""
assert not self.running, "Call engine.stop() before engine.kill()"
self.killed = True
def pause(self):
"""Pause the execution engine"""
self.paused = True
def unpause(self):
"""Resume the execution engine"""
self.paused = False
def is_idle(self):
return self.scheduler.is_idle() and self.downloader.is_idle() and \
self.scraper.is_idle()
def next_request(self, spider, now=False):
"""Scrape the next request for the spider passed.
The next request to be scraped is retrieved from the scheduler and
requested from the downloader.
The spider is closed if there are no more pages to scrape.
"""
if now:
self._next_request_calls.pop(spider, None)
elif spider not in self._next_request_calls:
call = reactor.callLater(0, self.next_request, spider, now=True)
self._next_request_calls[spider] = call
return call
else:
return
if self.paused:
return reactor.callLater(5, self.next_request, spider)
while not self._needs_backout(spider):
if not self._next_request(spider):
break
if self.spider_is_idle(spider):
self._spider_idle(spider)
def _needs_backout(self, spider):
return not self.running \
or self.spider_is_closed(spider) \
or self.downloader.sites[spider].needs_backout() \
or self.scraper.sites[spider].needs_backout()
def _next_request(self, spider):
# Next pending request from scheduler
request, deferred = self.scheduler.next_request(spider)
if request:
dwld = mustbe_deferred(self.download, request, spider)
dwld.chainDeferred(deferred).addBoth(lambda _: deferred)
dwld.addErrback(log.err, "Unhandled error on engine._next_request()",
spider=spider)
return dwld
def spider_is_idle(self, spider):
scraper_idle = spider in self.scraper.sites \
and self.scraper.sites[spider].is_idle()
#.........這裏部分代碼省略.........
示例2: ExecutionEngine
# 需要導入模塊: from scrapy.core.scraper import Scraper [as 別名]
# 或者: from scrapy.core.scraper.Scraper import is_idle [as 別名]
class ExecutionEngine(object):
def __init__(self, settings, spider_closed_callback):
self.settings = settings
self.closing = {} # dict (spider -> reason) of spiders being closed
self.closing_dfds = {} # dict (spider -> deferred) of spiders being closed
self.running = False
self.paused = False
self._next_request_calls = {}
self.scheduler = load_object(settings['SCHEDULER'])()
self.downloader = Downloader()
self.scraper = Scraper(self, self.settings)
self._spider_closed_callback = spider_closed_callback
@defer.inlineCallbacks
def start(self):
"""Start the execution engine"""
assert not self.running, "Engine already running"
self.start_time = time()
yield send_catch_log_deferred(signal=signals.engine_started)
self.running = True
def stop(self):
"""Stop the execution engine gracefully"""
assert self.running, "Engine not running"
self.running = False
dfd = self._close_all_spiders()
return dfd.addBoth(lambda _: self._finish_stopping_engine())
def pause(self):
"""Pause the execution engine"""
self.paused = True
def unpause(self):
"""Resume the execution engine"""
self.paused = False
def is_idle(self):
return self.scheduler.is_idle() and self.downloader.is_idle() and \
self.scraper.is_idle()
def next_request(self, spider, now=False):
"""Scrape the next request for the spider passed.
The next request to be scraped is retrieved from the scheduler and
requested from the downloader.
The spider is closed if there are no more pages to scrape.
"""
if now:
self._next_request_calls.pop(spider, None)
elif spider not in self._next_request_calls:
call = reactor.callLater(0, self.next_request, spider, now=True)
self._next_request_calls[spider] = call
return call
else:
return
if self.paused:
return reactor.callLater(5, self.next_request, spider)
while not self._needs_backout(spider):
if not self._next_request(spider):
break
if self.spider_is_idle(spider):
self._spider_idle(spider)
def _needs_backout(self, spider):
return not self.running \
or self.spider_is_closed(spider) \
or self.downloader.sites[spider].needs_backout() \
or self.scraper.sites[spider].needs_backout()
def _next_request(self, spider):
# Next pending request from scheduler
request, deferred = self.scheduler.next_request(spider)
if request:
dwld = mustbe_deferred(self.download, request, spider)
dwld.chainDeferred(deferred).addBoth(lambda _: deferred)
dwld.addErrback(log.err, "Unhandled error on engine._next_request()",
spider=spider)
return dwld
def spider_is_idle(self, spider):
scraper_idle = spider in self.scraper.sites \
and self.scraper.sites[spider].is_idle()
pending = self.scheduler.spider_has_pending_requests(spider)
downloading = spider in self.downloader.sites \
and self.downloader.sites[spider].active
return scraper_idle and not (pending or downloading)
def spider_is_closed(self, spider):
"""Return True if the spider is fully closed (ie. not even in the
closing stage)"""
return spider not in self.downloader.sites
def spider_is_open(self, spider):
"""Return True if the spider is fully opened (ie. not in closing
stage)"""
#.........這裏部分代碼省略.........
示例3: ExecutionEngine
# 需要導入模塊: from scrapy.core.scraper import Scraper [as 別名]
# 或者: from scrapy.core.scraper.Scraper import is_idle [as 別名]
class ExecutionEngine(object):
def __init__(self, settings, spider_closed_callback):
self.settings = settings
self.slots = {}
self.running = False
self.paused = False
self._next_request_calls = {}
self.scheduler = load_object(settings['SCHEDULER'])()
self.downloader = Downloader()
self.scraper = Scraper(self, self.settings)
self._spider_closed_callback = spider_closed_callback
@defer.inlineCallbacks
def start(self):
"""Start the execution engine"""
assert not self.running, "Engine already running"
self.start_time = time()
yield send_catch_log_deferred(signal=signals.engine_started)
self.running = True
def stop(self):
"""Stop the execution engine gracefully"""
assert self.running, "Engine not running"
self.running = False
dfd = self._close_all_spiders()
return dfd.addBoth(lambda _: self._finish_stopping_engine())
def pause(self):
"""Pause the execution engine"""
self.paused = True
def unpause(self):
"""Resume the execution engine"""
self.paused = False
def is_idle(self):
return self.scheduler.is_idle() and self.downloader.is_idle() and \
self.scraper.is_idle()
def next_request(self, spider, now=False):
"""Scrape the next request for the spider passed.
The next request to be scraped is retrieved from the scheduler and
requested from the downloader.
The spider is closed if there are no more pages to scrape.
"""
if now:
self._next_request_calls.pop(spider, None)
elif spider not in self._next_request_calls:
call = reactor.callLater(0, self.next_request, spider, now=True)
self._next_request_calls[spider] = call
return call
else:
return
if self.paused:
return reactor.callLater(5, self.next_request, spider)
while not self._needs_backout(spider):
if not self._next_request(spider):
break
if self.spider_is_idle(spider):
self._spider_idle(spider)
def _needs_backout(self, spider):
slot = self.slots[spider]
return not self.running \
or slot.closing \
or self.spider_is_closed(spider) \
or self.downloader.sites[spider].needs_backout() \
or self.scraper.sites[spider].needs_backout()
def _next_request(self, spider):
request = self.scheduler.next_request(spider)
if not request:
return
d = self._download(request, spider)
d.addBoth(self._handle_downloader_output, request, spider)
d.addErrback(log.msg, spider=spider)
slot = self.slots[spider]
d.addBoth(lambda _: slot.remove_request(request))
d.addErrback(log.msg, spider=spider)
d.addBoth(lambda _: self.next_request(spider))
return d
def _handle_downloader_output(self, response, request, spider):
assert isinstance(response, (Request, Response, Failure)), response
# downloader middleware can return requests (for example, redirects)
if isinstance(response, Request):
self.crawl(response, spider)
return
# response is a Response or Failure
d = defer.Deferred()
d.addBoth(self.scraper.enqueue_scrape, request, spider)
d.addErrback(log.err, spider=spider)
if isinstance(response, Failure):
d.errback(response)
#.........這裏部分代碼省略.........