I have a controller action in aiohttp application.
async def handler_message(request): try: content = await request.json() perform_message(x,y,z) except (RuntimeError): print("error in perform fb message") finally: return web.Response(text="Done")
perform_message
is async function. Now, when I call action I want that my action return as soon as possible and perform_message
put in event loop.
In this way, perform_message
isn't executed
The current method calls an async method that returns a Task or a Task<TResult> and doesn't apply the Await operator to the result. The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete.
There are a few alternative async/await transpilers to Regenerator, which take async code and attempt to convert to more traditional . then and . catch notation. In my experience, these transpilers work pretty well for simple functions, which await then return , perhaps with a try/catch block at most.
Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.
One way would be to use create_task
function:
import asyncio async def handler_message(request): ... loop = asyncio.get_event_loop() loop.create_task(perform_message(x,y,z)) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With