在python中异步获取和存储图像 [英] Asynchronously get and store images in python
问题描述
以下代码是非异步代码示例,有没有办法异步获取图片?
The following code is a sample of non-asynchronous code, is there any way to get the images asynchronously?
import urllib
for x in range(0,10):
urllib.urlretrieve("http://test.com/file %s.png" % (x), "temp/file %s.png" % (x))
我也看过 Grequests 库,但我不知道这是否可行或如何从文档中执行此操作.
I have also seen the Grequests library but I couldn't figure much if that is possible or how to do it from the documentation.
推荐答案
您不需要任何第三方库.只需为每个请求创建一个线程,启动线程,然后等待所有线程在后台完成,或者在下载图像时继续您的应用程序.
You don't need any third party library. Just create a thread for every request, start the threads, and then wait for all of them to finish in the background, or continue your application while the images are being downloaded.
import threading
results = []
def getter(url, dest):
results.append(urllib.urlretreave(url, dest))
threads = []
for x in range(0,10):
t = threading.Thread(target=getter, args=('http://test.com/file %s.png' % x,
'temp/file %s.png' % x))
t.start()
threads.append(t)
# wait for all threads to finish
# You can continue doing whatever you want and
# join the threads when you finally need the results.
# They will fatch your urls in the background without
# blocking your main application.
map(lambda t: t.join(), threads)
或者,您可以创建一个线程池,从队列中获取 urls
和 dests
.
Optionally you can create a thread pool that will get urls
and dests
from a queue.
如果您使用的是 Python 3,它已经在 futures
模块.
If you're using Python 3 it's already implemented for you in the futures
module.
这篇关于在python中异步获取和存储图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!