python 异步方式实现同时请求多个接口

import json

import requests


token = ""
callbackUrl = ""
def demo():

    import asyncio

    async def outExpense():

        expenseUrl = ""
        header = {
     
            "content-type": "application/json",
            "token": token
        }
        outPatientData = {
     
            
            }
        }
       
        para = json.dumps(outPatientData)
        requests_post = requests.post(expenseUrl, data=para, headers=header)
        print("1")
        await asyncio.sleep(3)

    async def inpExpense():
        expenseUrl = ""
        header = {
     
            "content-type": "application/json",
            "token":token
        }
        outPatientData = {
     
            }
        }
        
        para = json.dumps(outPatientData)
    
        requests_post = requests.post(expenseUrl, data=para, headers=header)
        print("2")
        await asyncio.sleep(3)
loop = asyncio.events.get_event_loop()

    tasks= [
        outExpense(),
        inpExpense()
    ]

    loop.run_until_complete((asyncio.wait(tasks)))

    loop.close()

if __name__ == '__main__':
    demo()

url和json数据出于项目保密没有贴上去,添加好url和数据即可使用。

你可能感兴趣的:(python 异步方式实现同时请求多个接口)