简体   繁体   English

python urllib2.urlopen(url)进程块

[英]python urllib2.urlopen(url) process block

I am using urllib2.urlopen() and my process is getting blocked 我正在使用urllib2.urlopen()并且我的进程被阻止了

I am aware that urllib2.urlopen() has default timeout. 我知道urllib2.urlopen()具有默认超时。

How to make the call unblockable? 如何使通话畅通无阻?

The backtrace is 回溯是

(gdb) bt 
#0 0x0000003c6200dc35 in recv () from /lib64/libpthread.so.0 
#1 0x00002b88add08137 in ?? () from /usr/lib64/python2.6/lib-dynload/_socketmodule.so 
#2 0x00002b88add0830e in ?? () from /usr/lib64/python2.6/lib-dynload/_socketmodule.so 
#3 0x000000310b2d8e19 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.6.so.1.0

If your problem is that you need to urllib to finish reading 如果您的问题是您需要urllib完成阅读

read() operation is blocking operation in Python. read()操作正在阻止Python中的操作。

If you want to create asynchronous requests 如果要创建异步请求

If your problem is need to set timeout 如果您的问题需要设置超时

Again, use requests library as mentioned above. 同样,使用如上所述的requests库。

You can try using strace (or similar) tool to figure out what the actual system call is that is blocking your python script, eg on linux: $ strace python yourscript.py 您可以尝试使用strace (或类似工具)工具找出阻止您的python脚本的实际系统调用,例如在linux上: $ strace python yourscript.py

yourscript.py: yourscript.py:

from urllib2 import urlopen
urlopen("http://somesite.local/foobar.html")

$ strace python yourscript.py

... lots of system call stripped ...
socket(PF_INET, SOCK_STREAM, IPPROTO_TCP) = 3
connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("127.0.0.1")}, 16

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

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