python请求异步触发并忘记

时间:2020-03-06 12:03:43

标签: python asynchronous python-requests python-asyncio

我正在尝试实现一种机制,“触发并忘记”我的请求并返回到调用函数,但是我的函数始终等待响应。

import requests

class Res():
    def process_data(self, data):
        # some data process
        plan = {'data':'test'}
        # Fire and Forget i dont care about the return value
        asyncio.run(self.fire(plan))

        # Im tryting to return the response before the response from the above call
        response = {'rid': '12345'}
        return response


    async def fire(self,message):
        print("async_foo done")
        payload = json.dumps(message, indent=4, default=lambda x: x.__dict__)
        headers = {'content-type': 'application/json'}
        async with aiohttp.ClientSession() as session:
            async with session.post(RUNTIME_URL, data=payload, headers=headers) as resp:
                print(resp.status)
                print(await resp.text())

能帮上忙吗,我不确定这里缺少什么。

0 个答案:

没有答案