简体   繁体   English

如何验证连接是否与 python requests.session 一起重用?

[英]how to verify connection is reused with python requests.session?

I'd like to use requests 's session to reuse connections in django.我想使用 requests 的 session 来重用 django 中的连接。

Reusing connections in Django with Python Requests 使用 Python 请求重用 Django 中的连接

says I only need to declare it in global and access it.说我只需要在全局中声明它并访问它。
However I doubt it is working as expected because it didn't get any faster in my test.但是我怀疑它是否按预期工作,因为它在我的测试中并没有变得更快。

Is there a way to see if connection is actually reused as described here?有没有办法查看连接是否真的被重用,如此处所述?

http://docs.python-requests.org/en/latest/user/advanced/ http://docs.python-requests.org/en/latest/user/advanced/

Django spawns separate thread for each requests and I think it defeats the mechanism to share the connection. Django 为每个请求生成单独的线程,我认为它破坏了共享连接的机制。 (because session won't be shared across multiple threads).. this is my hypothesis.. (因为 session 不会在多个线程之间共享)..这是我的假设..

Maybe you already reuse connections 也许您已经重用了连接

Any requests that you make within a session will automatically reuse the appropriate connection! 您在会话中发出的任何请求都将自动重用适当的连接!

via http://docs.python-requests.org/en/latest/user/advanced/#keep-alive 通过http://docs.python-requests.org/en/latest/user/advanced/#keep-alive

But if you need to be sure you can put some debug prints inside urllib3 requests/packages/urllib3/poolmanager.py 但是,如果需要确保可以在urllib3 requests/packages/urllib3/poolmanager.py放入一些调试打印requests/packages/urllib3/poolmanager.py

You can try increasing your logging verbosity, then look out for logs that look like:您可以尝试增加日志记录的详细程度,然后查看如下所示的日志:

"Starting new HTTPS connection (1): some.url:port"

This is how to make global logging more verbose:这是使全局日志记录更详细的方法:

import logging
logging.basicConfig(level=logging.DEBUG, format="%(message)s")

If the connection is being reused, you will only see one of those messages for any given url.如果连接被重用,您将只会看到任何给定 url 的这些消息之一。 If it is not being reused, you will see a different one each time a connection is established to the same url.如果它没有被重用,每次与同一个 url 建立连接时,您都会看到不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM