After perusing many docs on AsyncIO
and articles I still could not find an answer to this : Run a function asynchronously (without using a thread) and also ensure the function calling this async function continues its execution.
Pseudo - code :
async def functionAsync(p):
#...
#perform intensive calculations
#...
print ("Async loop done")
def functionNormal():
p = ""
functionAsync(p)
return ("Main loop ended")
print ("Start Code")
print functionNormal()
Expected Output :
Start code
Main loop ended
Async loop done
Searched examples where loop.run_until_complete
is used, but that will not return the print value of functionNormal()
as it is blocking in nature.
async def
) for all your code. But your requirement is to have a sync function executed concurrently with async code, and that will certainly require multiple threads or fibers.asyncio.new_event_loop()
..And yes you are right, the sync code should continue running and go to the next line of code as shown in the example.new_event_loop
only allocates an event loop. To actually run async code in it, you must userun_until_complete
orrun_forever
, which blocks the current thread - so you need an additional thread to run sync code concurrently with async code. It will never work without threads.