【发布时间】:2016-03-11 08:47:05
【问题描述】:
为什么下面的代码会阻塞在 cc.start() 上? crawler.py 包含类似于http://doc.scrapy.org/en/latest/topics/practices.html#run-from-script的代码
import scrapy
import threading
from subprocess import Popen, PIPE
def worker():
crawler = Popen('python crawler.py', stdout=PIPE, stderr=PIPE, shell=True)
while True:
line = crawler.stderr.readline()
print(line.strip())
cc = threading.Thread(target=worker())
cc.setDaemon(True)
cc.start()
print "Here" # This is not printed
# Do more stuff
crawler.py 包含以下代码:
from scrapy.crawler import CrawlerProcess
import scrapy
class MySpider(scrapy.Spider):
name = 'stackoverflow'
start_urls = ['http://stackoverflow.com/questions?sort=votes']
def parse(self, response):
for href in response.css('.question-summary h3 a::attr(href)'):
full_url = response.urljoin(href.extract())
yield scrapy.Request(full_url, callback=self.parse_question)
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
process.crawl(MySpider)
process.start() # the script will block here until the crawling is finished
【问题讨论】:
-
代码本身会中断,因为你没有使用
subprocess.Popen。 -
对不起,我后来添加了导入语句,我修复了它。这不是问题。
-
那么我建议你相应地更新你的代码以排除任何可能的原因。
-
另外,您可能想通过
Popen(...)解释为什么要运行python 脚本,因为这根本没有任何意义。您可以轻松地将其包装在import crawler中。 -
我添加了被调用脚本的内容。我使用 Popen 是因为我想将爬虫作为项目的一部分运行,然后在此之后做更多的事情(为一件事捕获数据包)
标签: python-2.7 subprocess python-multithreading