Issue
So I have a list of image url's that I want to iterate over with the requests library and download all the images to a directory.
def get_image(url, image_name):
path = pathlib.Path('/path/to/some/directory')
response = requests.get(url, stream=True)
with open('{}/{}.png'.format(path, image_name), 'wb') as file:
for block in response.iter_content(1024):
file.write(block)
for url in urls:
get_image(url, image_name)
Now, is there no way I could create a decorator to make a function a callback to run once a response is returned for a specific asynchronous request?
Solution
If you want multiple concurrent requests
+ callbacks you can use module like grequests. It has nothing to do with asyncio
.
asyncio
- is all about to avoid using of callbacks (to avoid callback hell) and make writing of asynchronous code as easy as synchronous one.
If you decide to try asyncio
you should either use aiohttp
client instead of requests
(this is preferred way) or run requests
in thread pool managed by asyncio. Example of both ways can be found here.
Answered By - Mikhail Gerasimov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.