为什么python打印延迟? [英] Why python print is delayed?
问题描述
我正在尝试使用请求下载文件,并在每次检索 100k 大小的文件时打印一个点,但最后打印出所有点.见代码.
I am trying to download file using requests, and print a dot every time retrieve 100k size of file, but all the dots is printed out at the end. See code.
with open(file_name,'wb') as file:
print("begin downloading, please wait...")
respond_file = requests.get(file_url,stream=True)
size = len(respond_file.content)//1000000
#the next line will not be printed until file is downloaded
print("the file size is "+ str(size) +"MB")
for chunk in respond_file.iter_content(102400):
file.write(chunk)
#print('',end='.')
sys.stdout.write('.')
sys.stdout.flush()
print("")
推荐答案
您正在此处访问 request.content
:
size = len(respond_file.content)//1000000
访问该属性会强制下载整个响应,对于大型响应,这需要一些时间.使用 int(respond_file.headers['content-length'])
代替:
Accessing that property forces the whole response to be downloaded, and for large responses this takes some time. Use int(respond_file.headers['content-length'])
instead:
size = int(respond_file.headers['content-length']) // 1000000
Content-Length
标头由服务器提供,由于它是标头的一部分,您无需先下载所有内容即可访问该信息.
The Content-Length
header is provided by the server and since it is part of the headers you have access to that information without downloading all of the content first.
如果服务器选择使用 Transfer-Encoding: chunked
来流式传输响应,则无需设置 Content-Length
标头;您可能需要考虑到这一点:
If the server chooses to use Transfer-Encoding: chunked
to stream the response, no Content-Length
header has to be set; you may need to take that into account:
content_length = respond_file.headers.get('content-length', None)
size_in_kb = '{}KB'.format(int(content_length) // 1024) if content_length else 'Unknown'
print("the file size is", size_in_kb)
其中以千字节为单位的大小是通过将长度除以 1024 来计算的,而不是 100 万.
where the size in kilobytes is calculated by dividing the length by 1024, not 1 million.
或者,在单独的 HEAD 请求中询问大小(仅获取标头):
Alternatively, ask for the size in a separate HEAD request (only fetching the headers):
head_response = requests.get(file_url)
size = int(head_response.headers.get('content-length', 0))
这篇关于为什么python打印延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!