Await Outside Async In Async/await
Was using the multiprocessing process before async, to test which is faster I am trying to run the code with async, but it gives me an error saying: 'await' outside async. My code:
Solution 1:
sum_
is a separate function, as far as Python is concerned. If you want to await
inside a function, it needs to be async
. And if you want to call an async
function, you need to await
it.
async def sum(name, numbers):
async def sum_(numbers): # <-- This function needs to be async
total = 0
print(f'Task {name}: Computing {total}+{number}')
await sleep()
total += number
print(f'Task {name}: Sum = {total}\n')
for number in numbers:
await sum_(numbers) # <-- And we need to await it here
Post a Comment for "Await Outside Async In Async/await"