使用 asyncio 通过 python 中的 post 请求将相同的数据发送到 3 个独立的端点

问题描述 投票:0回答:1

我很难使用 asyncio 将相同的数据有效负载发送到 3 个不同的端点。请注意,这些是帖子请求。我是 asyncio 的新手,所以请多多包涵。由于我有 3 个不同的端点(所有端点都使用相同的数据负载),我认为使用

loop.run_in_executor
会起作用。

这是我的示例代码:

import asyncio
import requests

async def main():
    loop = asyncio.get_event_loop()
    model1 = loop.run_in_executor(None, lambda : requests.post(url = url1, 
                                                              json = sample_data, 
                                                              headers = header1))
    
    model2 = loop.run_in_executor(None, lambda : requests.post(url = url2, 
                                                              json = sample_data, 
                                                              headers = header2))
    
    model3 = loop.run_in_executor(None, lambda : requests.post(url = url3, 
                                                               json = sample_data, 
                                                               headers = header3))
    
    model1_response = await model1
    model2_response = await model2
    model3_response = await model3

    #print(dir(model1_response)) 
    
tm1 = time.perf_counter()

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

tm2 = time.perf_counter()
print(f'Total time elapsed: {tm2-tm1:0.2f} seconds')

我怀疑我破解了这个表面,因为我收到错误

RuntimeError: This event loop is already running
。请帮助

python-3.x post python-requests python-asyncio
1个回答
0
投票

您可以尝试

asyncio.gather()
并发运行任务:

import time
import asyncio

import requests

url1 = 'https://www.google.com'
url2 = 'https://stackoverflow.com'
url3 = 'https://www.yahoo.com'

async def main():
    loop = asyncio.get_running_loop()

    model1 = loop.run_in_executor(
        None, lambda: requests.get(url=url1)
    )

    model2 = loop.run_in_executor(
        None, lambda: requests.get(url=url2)
    )

    model3 = loop.run_in_executor(
        None, lambda: requests.get(url=url3)
    )

    responses = await asyncio.gather(model1, model2, model3)
    for r in responses:
        print(r.text[:10])  # print first 10 characters from each response

tm1 = time.perf_counter()

asyncio.run(main())

tm2 = time.perf_counter()
print(f"Total time elapsed: {tm2-tm1:0.2f} seconds")

印花:

<!doctype 
<!DOCTYPE 
<!doctype 
Total time elapsed: 0.67 seconds

注意: 我建议使用异步库来处理 HTTP 请求,例如

aiohttp
而不是
requests
.

© www.soinside.com 2019 - 2024. All rights reserved.