1

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:

import asyncio import time async def sleep(): print(f'Time: {time.time() - start:.2f}') await asyncio.sleep(1) async def sum(name, numbers): def sum_(numbers): total = 0 print(f'Task {name}: Computing {total}+{number}') await sleep() total += number print(f'Task {name}: Sum = {total}\n') for number in numbers: sum_(numbers) start = time.time() loop = asyncio.get_event_loop() tasks = [ loop.create_task(sum("A", [1, 2])), loop.create_task(sum("B", [1, 2, 3])), ] loop.run_until_complete(asyncio.wait(tasks)) loop.close() end = time.time() print(f'Time: {end-start:.2f} sec') 

Please note: This is just an example of code, in original code I cannot do as per the below:

 for number in numbers: sum_(numbers) await sleep() 

Testing asyncio as advised on this comment

4
  • sum_() is not an async function, you can't use await in it. Change to async def sum_(numbers): Commented Aug 22, 2021 at 5:11
  • I tried by doing something: async def on_ticks(ws, ticks): #await sleep() inside this function and await kws.on_ticks = on_ticks, it gave me error: SyntaxError: cannot assign to await expression, full code @Barmar Commented Aug 22, 2021 at 5:22
  • It should be kws.on_ticks = await on_ticks() You use await to call an asynchronous function. Commented Aug 22, 2021 at 5:26
  • @Barmar this time it gave object function can't be used in 'await' expression, looks like asyncio cannot be used on this question code Commented Aug 22, 2021 at 5:33

1 Answer 1

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 
Sign up to request clarification or add additional context in comments.

3 Comments

I tried by doing something: async def on_ticks(ws, ticks): #await sleep() inside this function and await kws.on_ticks = on_ticks, it gave me error: SyntaxError: cannot assign to await expression, full code
Sorry, but I fail to see what that comment has to do with this question. await can't be used on assignment expressions, sure, but that's not remotely what was asked here. If you have another question, consider making a separate post about it.
there's already a question for it which gave me a solution to use asyncio and was doing the same. Have already mentioned in the question: Testing asyncio as advised on this comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.