|
|
|
|
|
by Twirrim
3217 days ago
|
|
For OP's case, I wouldn't have jumped to async, but instead either to multithreading or multiprocessing. Pool().map makes this really trivial. Taking their example, and tweaking it slightly: import requests
from multiprocessing import Pool
def fetch_things():
pool = Pool() # defaults to number of CPUs
urls = ['https://example.com/api/object/1',
'https://example.com/api/object/2',
'https://example.com/api/object/3']
return pool.map(requests.get, urls)
print(fetch_things())
Output (because those URLs are nonsense...): [<Response [404]>, <Response [404]>, <Response [404]>]
It's just as easy to do it in threading. Just switch that "from multiprocessing import Pool" with "from multiprocessing.dummy import Pool" |
|