在异步 def 循环执行器中完成请求时如何添加请求参数?
Posted
技术标签:
【中文标题】在异步 def 循环执行器中完成请求时如何添加请求参数?【英文标题】:How to add requests parameters when request is done in async def loop executor? 【发布时间】:2019-01-18 15:38:33 【问题描述】:如何将verify
和proxies
等参数添加到下面的requests.get
?
在非异步设置中,我只会做requests.get(url, proxies='some_proxy', verify=False)
,但我不知道如何将其放在下面。
import asyncio
import concurrent.futures
import requests
ids = [2048854772, 2042055933, 2036234693, 2007740886, 2006259847, 2003100744]
token = '111111'
max_workers = len(ids)
async def main():
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
loop = asyncio.get_event_loop()
futures = [
loop.run_in_executor(
executor,
requests.get,
'https://www.strava.com/api/v3/activities/id?include_all_efforts=true&access_token=token'.format(id=id, token=token)
)
for id in ids
]
for response in await asyncio.gather(*futures):
print(response.text)
pass
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
【问题讨论】:
这在文档中得到了回答:docs.python.org/3/library/… 【参考方案1】:您可以使用partial:
from functools import partial
def sum(a, b):
return a + b
sum_with_two = partial(sum, 2)
sum_with_two(5)
>>> 7
sum_two_and_four = partial(sum, 2, 4)
sum_two_and_four()
>>> 6
在你的情况下:
my_request = partial(requests.get, proxies='...', verify=False)
loop.run_in_executor(
executor,
my_request, # Arguments of the partials will be used
'...url...'
)
【讨论】:
以上是关于在异步 def 循环执行器中完成请求时如何添加请求参数?的主要内容,如果未能解决你的问题,请参考以下文章