Comments (4)
Thanks, didn't know crawler can automatically await on async functions connected via signals. The following code works as expected:
import asyncio
import logging
from scrapy import signals
import aiohttp
class AsynctestPipeline:
async def ainit(self):
logging.critical('async init')
self.client = aiohttp.ClientSession()
self.something = await self.client.get('https://scrapy.org/')
logging.critical('async initialised')
async def adel(self):
logging.critical('async closing resources')
await self.client.close()
logging.critical('async resources closed')
@classmethod
def from_crawler(cls, crawler):
p = cls()
crawler.signals.connect(p.ainit, signal=signals.spider_opened)
crawler.signals.connect(p.adel, signal=signals.spider_closed)
return p
async def process_item(self, item, spider):
logging.critical('executing async task')
await asyncio.sleep(10)
logging.critical('async task done')
logging.critical(self.something)
return item
from scrapy.
Only process item
has coroutine support. Try using the spider_closed
signal instead.
from scrapy.
but why then open_spider
works?
from scrapy.
My guess: both work, only that the spider closes before the close one gets executed. Do you get any mentions in the standard output about unawaited futures? If you add a long sleep on the close spider signal, the close one might work. In any case, it is not intended to work in open_spider
either, so I strongly suggest not to do that either, there is also an spider_open
signal.
from scrapy.
Related Issues (20)
- Implement get_import _path
- Replace urlparse with urlparse_cached where possible
- Explanation of the robots.txt exclusion standard in DownloaderMiddleware.robotstxt.py HOT 1
- Use `defusedxml.xmlrpc`
- AttributeError: 'Decompressor' object has no attribute 'process' HOT 8
- Fix and re-enable `unnecessary-comprehension` and `use-dict-literal` pylint tags
- Investigate speeding up `MockServer()` HOT 3
- Proxy tests don't use tests/keys/mitmproxy-ca.pem HOT 2
- Add brotlicffi support
- scrapy.pqueues.ScrapyPriorityQueue HOT 8
- Remove tests/requirements.txt
- Add an extra-deps job for pypy
- Document the SpiderState extension
- WindowsRunSpiderCommandTest isn't skipped properly in the pinned envs
- More documentation needed about the robots.txt protocol HOT 3
- GZipPlugin does not work with S3 HOT 3
- AttributeError: 'Decompressor' object has no attribute 'process' HOT 1
- Handle robots.txt files not utf-8 encoded HOT 3
- SitemapSpider will ignore sitemap with URLs like https://website.com/filename.xml?from=7155352010944&to=7482320519360 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scrapy.