我有一个网址:“https://findicons.com/files/icons/2787/beautiful_flat_icons/128/running.png”
我想获取图像并将其写入文件,我编写代码如下:
xAxis.setLabelRotationAngle(45f);
我的问题是,如果我有很多网址要下载,我怎么能通过龙卷风的协程实现呢?
答案 0 :(得分:1)
这不是整个代码,只是我在5分钟内提出的内容,但它应该为您提供足够的信息以满足您的要求。如果您有任何问题或需要进一步说明,请告诉我。
from tornado import gen, httpclient, ioloop
@gen.coroutine
def main():
client = httpclient.AsyncHTTPClient()
response = yield client.fetch(
'https://findicons.com/files/icons/2787/beautiful_flat_icons/128/running.png',
download_image,
follow_redirects = True)
@gen.coroutine
def download_image(response):
buffer_size = 1024
filename = response.request.url.split('/')[-1] # this is not always reliable
with open(filename, 'ab') as img:
while True:
chunk = response.buffer.read(buffer_size)
if chunk == '':
break
img.write(chunk)
yield
ioloop.IOLoop.current().run_sync(main)