如果 url 太大,urllib 会冻结! [英] urllib freeze if url is too big !

查看:19
本文介绍了如果 url 太大,urllib 会冻结!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我尝试使用 urllib 打开一个 url,但问题是文件太大,所以当我打开 url 时,python 冻结,我也使用 wxpython,当我打开 url 时它也会冻结打开网址时,我的 CPU 几乎达到 100%

ok im trying to open a url using urllib but the problem is that the file is too big, so when i open the url python freezes, im also using wxpython which also freezes when i open the url my cpu goes to almost 100% when the url is opened

有什么解决办法吗?有没有办法我可以分块打开网址,并且可能在那里有一个 time.sleep(0.5) 这样它就不会冻结?这是我的代码:

any solutions ? is there a way i can open the url in chunks and maybe have a time.sleep(0.5) in there so it does not freeze ? this is my code :

f = open("hello.txt",'wb')
datatowrite = urllib.urlopen(link).read()
f.write(datatowrite)
f.close()

谢谢

推荐答案

您希望将下载拆分到一个单独的线程中,以便您的 UI 线程继续工作,而下载线程则单独执行工作.这样你就不会在下载时冻结".

You want to split the download into a separate thread, so your UI thread continues to work while the download thread does the work separately. That way you don't get the "freeze" while the download happens.

在此处阅读有关线程的更多信息:

Read more about threading here:

http://docs.python.org/library/threading.html

或者,您可以使用系统使用 curl 或 wget 在 python 之外下载文件.

Alternatively, you could use the system to download the file outside of python using curl or wget.

这篇关于如果 url 太大,urllib 会冻结!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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