Python请求模块在特定机器上非常慢 [英] Python requests module is very slow on specific machine

查看:30
本文介绍了Python请求模块在特定机器上非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到过在某些机器上和特定用户上执行 Python 请求的速度太慢,而其他工具(例如 curl)则非常快.奇怪的是,如果以另一个用户身份运行脚本,那么它会按预期运行.如果我在我的机器(Windows 或 Linux)上运行脚本,那么它也会按预期运行.有问题的机器是 Hyper-V 上的 Windows 2008 服务器.我通常使用 POST 请求,但 POST 和 GET 都会受到影响.为了演示,我创建了带有 GET 请求的简单脚本.所有请求大约需要 4.8 秒,但应该需要大约 0.03 秒(虚拟机没有那么强大).

I've experienced too slow execution of Python requests on some machines and with specific user while other tools (for instance curl) are quite fast. Strange thing is that if run the script as another user then it runs as expected. If I run the script on my machine (both Windows or Linux) then it runs as expected too. Problematic machines are Windows 2008 servers on Hyper-V. I usually use POST request but both POST and GET are affected. For the demonstration I've created simple script with GET request. All requests take about 4.8s but it should take about 0.03s (virtual machines are not so powerful).

[imports and logging configuration omitted]

log.info("Started ...")

start = time.time()
response1 = requests.get("http://10.50.30.216:8080/sps/api/version")
assert response1.status_code == codes.OK
log.info("Using requests: %.3fs" % (time.time() - start))

start = time.time()
conn = httplib.HTTPConnection("10.50.30.216:8080")
conn.request("GET", "/sps/api/version")
response2 = conn.getresponse()
assert response2.status == codes.OK
log.info("Using httplib: %.3fs" % (time.time() - start))

log.info("Finished ...")

以有问题的用户身份登录时的输出(不幸的是我必须使用该用户).看到 requests 模块在打开连接之前等待 4.523 秒,而 httplib 模块立即继续.

Output when logged as problematic user (unfortunately I must use that user). See that requests module waits 4.523s before opening a connection while httplib module proceeds immediately.

2015-09-11 14:50:00,832 - INFO - myscript - Started ...
2015-09-11 14:50:05,355 - INFO - requests.packages.urllib3.connectionpool - Starting new HTTP connection (1): 10.50.30.216
2015-09-11 14:50:05,364 - DEBUG - requests.packages.urllib3.connectionpool - "GET /sps/api/version HTTP/1.1" 200 None
2015-09-11 14:50:05,365 - INFO - myscript - Using requests: 4.533s
2015-09-11 14:50:05,374 - INFO - myscript - Using httplib: 0.008s
2015-09-11 14:50:05,375 - INFO - myscript - Finished ...

以其他用户身份登录时的输出.请注意,两个用户都具有管理员权限,但第二个用户只是临时的,并且只能在一台机器上使用,因此我无法通过切换用户来解决此问题.

Output when logged as another user. Note that both users have Administrator privileges but the second user is only temporary and only on one machine so I can't use solve this issue by switching users.

2015-09-11 14:57:45,789 - INFO - myscript - Started ...
2015-09-11 14:57:45,799 - INFO - requests.packages.urllib3.connectionpool - Starting new HTTP connection (1): 10.50.30.216
2015-09-11 14:57:45,806 - DEBUG - requests.packages.urllib3.connectionpool - "GET /sps/api/version HTTP/1.1" 200 None
2015-09-11 14:57:45,809 - INFO - myscript - Using requests: 0.021s
2015-09-11 14:57:45,815 - INFO - myscript - Using httplib: 0.004s
2015-09-11 14:57:45,815 - INFO - myscript - Finished ...

我读过 Python 请求很慢 #1Python 请求比 curl 慢 但它没有适用于我的问题.

I've read Python requests are slow #1 and Python requests are slower thann curl but it does not apply to my problem.

推荐答案

对于阅读本文并在其 URL 中使用 localhost 的任何人,我将其更改为 127.0.0.1.

For anyone reading this and using localhost in their URL, I resolved this issue by changing it to 127.0.0.1.

如果这解决了问题,那就是 DNS 问题,而不是请求问题.

If this resolves the problem, it's a DNS problem, and it's not a requests problem.

这篇关于Python请求模块在特定机器上非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆