Scrapy中间件订单
Scrapy 文档 说:
第一个 中间件是最接近的一个 发动机,最后一个更接近 到下载器。
决定分配给哪个订单 你的中间件看到 DOWNLOADER_MIDDLEWARES_BASE 设置 并根据位置选择一个值 你想插入中间件。这 顺序很重要,因为每个 中间件执行不同的操作 你的中间件可能依赖于 一些先前的(或后续的) 正在应用的中间件
我并不完全清楚较高的值是否会导致中间件 首先被执行,反之亦然。
例如
'myproject.middlewares.MW1': 543,
'myproject.middlewares.MW2': 542,
问题:
- 其中哪一个将首先执行?我的试验表明,MW2 将是第一。
- 订单的有效范围是多少? 0 - 999?
Scrapy documentation says :
the first
middleware is the one closer to the
engine and the last is the one closer
to the downloader.To decide which order to assign to
your middleware see the
DOWNLOADER_MIDDLEWARES_BASE setting
and pick a value according to where
you want to insert the middleware. The
order does matter because each
middleware performs a different action
and your middleware could depend on
some previous (or subsequent)
middleware being applied
I'm not entirely clear from this whether a higher value would result in a middleware
getting executed first or vice versa.
E.g.
'myproject.middlewares.MW1': 543,
'myproject.middlewares.MW2': 542,
Question :
- Which of these will be executed first? My trial says that MW2 would be first.
- What's the valid range for the orders ? 0 - 999 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这个问题已经得到解答,但实际上这是一件更复杂的事情——请求和响应以相反的顺序处理。
你可以这样想:
所以...如果我将我的中间件标记为数字1,它将是执行的第一个请求中间件和执行的最后一个响应中间件...如果我的中间件为901,它将是最后一个请求执行的中间件和执行的 FIRST 响应中间件(如果仅定义了默认中间件)。
事实上,答案是令人困惑。请求的开始位置最接近引擎(零),请求的结束位置最接近下载器(高数字)。响应的开始最接近下载器(高数字),响应的结束最接近引擎(零)。这就像从引擎中往返一样...这是来自 scrapy 的相关代码,使这一切变得如此有趣(从 MiddlewareManager 复制的 init 供参考,仅包含相关方法)
:可以看到,请求方法按排序顺序附加(数字较大的添加到后面),响应和异常方法插入在开头(数字较大的在前面)。
I know this has been answered, but really it's a more complicated thing -- requests and responses are handled in opposite order.
you can think of it like this:
so ... if i tag my middleware as number 1 it will be the FIRST request middleware executed and the LAST response middleware executed ... if my middleware as 901 it will be the LAST request middleware executed and the FIRST response middleware executed (if only the default middleware is defined).
really the answer is that it IS confusing. the start of the request is nearest the engine (at zero) and the end of the request is nearest the downloader (high number). the start of the response is nearest the downloader (high number) and the end of the response is nearest the engine (at zero). it's like a trip out and back from the engine ... here's the relevant code from scrapy that makes this all so fun (with init copied from MiddlewareManager for reference and only the relevant method included):
As you can see, request methods are appeneded in sorted order (higher number added to the back) and response and exception methods are inserted at the beginning (higher number is first).
正如您引用的文档:
因此,值为 542 的下载器 中间件 在值为 543 的中间件之前执行。这意味着首先
myproject.middlewares.MW1.process_request(request, Spider)
被调用,并在更改(如果需要)请求后,将其传递给下一个下载器中间件。该值为整数。
更新:
查看架构。
另外,完整的引用 :
因此,由于值是整数,因此它们具有 Python 整数范围。
As you quoted the docs:
So downloader middleware with value of 542 is executed before the middleware with value 543. It means first
myproject.middlewares.MW1.process_request(request, spider)
is called, and after it altered (if needed) the request, it is passed to the next downloader middleware.The value is an integer.
UPDATE:
Look at the architecture.
Also, the full quote:
So, as the values are integers, they have range of Python integers.