2010-10-08 148 views
1

我有客户端的Web界面长期运行的过程。我希望显示该进程的输出。与urllib.urlopen()效果很好,但它没有timeout参数。另一方面,urllib2.urlopen()输出被缓冲。有没有简单的方法来禁用该缓冲区?无缓冲urllib2.urlopen

+0

非常类似的问题在http://stackoverflow.com/questions/107705/python-output-buffering – synthesizerpatel 2010-10-08 08:40:30

+1

@synthesizerpatel:好吧,urlopen()返回与文件类接口的对象,但它不是一个文件。 – vartec 2010-10-08 08:51:10

回答

0

发生在我身上的一个快速入侵是使用urllib.urlopen()threading.Timer()来模拟超时。但这只是快速和肮脏的黑客攻击。当你只需要调用read()

可以定义一个大小阅读,从而禁用缓冲

0

urllib2缓冲。

例如:

import urllib2 

CHUNKSIZE = 80 

r = urllib2.urlopen('http://www.python.org') 
while True: 
    chunk = r.read(CHUNKSIZE) 
    if not chunk: 
     break 
    print(chunk) 

这将打印响应每个块从插座读取之后,直到接收到整个响应被缓存。