Python 如何在asyncio偶数循环中运行scrapy spider?
看来我走到了死胡同。有没有办法在asyncio循环中运行scrapy spider?例如,在下面的代码中:Python 如何在asyncio偶数循环中运行scrapy spider?,python,scrapy,Python,Scrapy,看来我走到了死胡同。有没有办法在asyncio循环中运行scrapy spider?例如,在下面的代码中: import asyncio from scrapy.crawler import CrawlerProcess from myscrapy import MySpider import scrapy async def do_some_work(): process = CrawlerProcess() await process.crawl(MySpider) lo
import asyncio
from scrapy.crawler import CrawlerProcess
from myscrapy import MySpider
import scrapy
async def do_some_work():
process = CrawlerProcess()
await process.crawl(MySpider)
loop = asyncio.get_even_loop()
loop.run_until_complete(do_some_work())
这就导致了我的错误:
raise TypeError('A Future, a coroutine or an awaitable is required')
TypeError: A Future, a coroutine or an awaitable is required
我知道在等待之后,应该会有另一次合作。有没有办法绕过它,让它仍然异步工作?谢谢整个scrapy都是同步代码。每当发生阻塞时,没有异步机制(协同路由)将正在运行的资源转回select循环。
主要的阻塞是网络请求。libs scrapy使用不支持asyncio。因此,也许您可以打开零碎的源代码来实现asyncio或aiohttp来替换原始的网络库,这样就可以了。然而,在这些LIB之上,还有复杂的twisted模块。(类似于asyncio,但速度不如asyncio快,因为python 2)。这可能比从头开始使用asyncio构建新框架更加困难。Scrapy目前不支持
async
语法
如果您需要在基于异步IO的代码中运行Scrapy,则需要像在异步函数中运行任何其他同步代码一样运行Scrapy
也就是说,这是未来可能会有的东西。在这个方向上已经有了一个解决方案,而且还有一个。为什么您认为它可以与Wait一起工作?并非所有方法都可以异步工作。这是不正确的。Scrapy使用Twisted的事件驱动网络,因此不完全同步。