Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I call an async function without await?

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

like image 331
Pasalino Avatar asked Jun 19 '17 12:06

Pasalino


People also ask

Can we call async method without await?

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.

What can I use instead of async await?

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.

How do you call async functions?

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.


1 Answers

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))     ... 
like image 136
freakish Avatar answered Oct 14 '22 18:10

freakish