如何在 Python 中进行非阻塞 URL 获取
我正在 Pyglet 中编写一个 GUI 应用程序,它必须显示来自互联网的数十到数百个缩略图。 现在,我正在使用 urllib.urlretrieve 来获取它们,但是每次都会阻塞,直到它们完成为止,并且一次只抓取一个。
我更喜欢并行下载它们,并在完成后立即显示它们,而不会在任何时候阻塞 GUI。 做这个的最好方式是什么?
我对线程了解不多,但看起来 threading 模块可能会有所帮助? 或者也许有一些我忽略的简单方法。
I am writing a GUI app in Pyglet that has to display tens to hundreds of thumbnails from the Internet. Right now, I am using urllib.urlretrieve to grab them, but this blocks each time until they are finished, and only grabs one at a time.
I would prefer to download them in parallel and have each one display as soon as it's finished, without blocking the GUI at any point. What is the best way to do this?
I don't know much about threads, but it looks like the threading module might help? Or perhaps there is some easy way I've overlooked.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可能会受益于
线程
或多处理
模块。 您实际上不需要自己创建所有这些基于 Thread 的类,有一个更简单的方法,使用 Pool.map:You'll probably benefit from
threading
ormultiprocessing
modules. You don't actually need to create all thoseThread
-based classes by yourself, there is a simpler method usingPool.map
:正如您所怀疑的,这是线程的完美情况。 这里是我发现的一个简短指南当我在 python 中做我自己的第一个线程处理时很有帮助。
As you suspected, this is a perfect situation for threading. Here is a short guide I found immensely helpful when doing my own first bit of threading in python.
正如您所指出的,您可以创建多个线程,每个线程负责执行 urlretrieve 操作。 这允许主线程不间断地继续。
这是关于Python中线程的教程:
http://heather.cs.ucdavis.edu/~matloff/Python/ PyThreads.pdf
As you rightly indicated, you could create a number of threads, each of which is responsible for performing urlretrieve operations. This allows the main thread to continue uninterrupted.
Here is a tutorial on threading in python:
http://heather.cs.ucdavis.edu/~matloff/Python/PyThreads.pdf
下面是如何使用 threading.Thread 的示例。 只需将类名替换为您自己的名称,并将 run 函数替换为您自己的函数即可。 请注意,线程非常适合像您这样的 IO 受限应用程序,并且确实可以加快速度。 在标准 python 中严格使用 pythong 线程进行计算并没有帮助,因为一次只有一个线程可以计算。
Here's an example of how to use threading.Thread. Just replace the class name with your own and the run function with your own. Note that threading is great for IO restricted applications like your's and can really speed it up. Using pythong threading strictly for computation in standard python doesn't help because only one thread can compute at a time.
您有以下选择:
我建议仅使用线程,除非您需要工业规模的获取器。
You have these choices:
I recommend just using threads unless you need an industrial scale fetcher.
您要么需要使用线程,要么需要使用异步网络库,例如 Twisted。 我怀疑在您的特定用例中使用线程可能会更简单。
You either need to use threads, or an asynchronous networking library such as Twisted. I suspect that using threads might be simpler in your particular use case.