2

All I need to do is write a simple async function that returns something. Something akin to

async def diss(lis):
  x = []
  for i in lis:
    x.append(i + 1) #the operation is arbitrary, but the input and output of a list is the desired result
  return x

lis = [1, 2, 3, 4]
res = await diss(lis)

However, this gives me a syntactical error at await diss(lis)

All I have found online were tutorials where something is printed inside the async function but when trying to return something, I always receive a coroutine or future object.

My goal is to essentially have the loop run asynchronously so to improve performance and then have something returned

2
  • Are you trying to await the coroutine outside of an event loop? You need to get or create an event loop and run the coroutine in it. The simplest way would be with asyncio.run Commented Sep 10, 2019 at 19:21
  • 1
    as per [stackoverflow.com/questions/52796630/… I found that python 3.6 asyncio does not support run Commented Sep 10, 2019 at 19:36

2 Answers 2

2

Here you go:

import asyncio


async def diss(lis):
  x = []
  for i in lis:
    x.append(i + 1)
  return x


async def main():
  lis = [1, 2, 3, 4]
  res = await diss(lis)
  return res


loop = asyncio.get_event_loop()
res = loop.run_until_complete(main())
print(res)

Make sure you understand why and when to use asyncio in the first place. For example, there's no point to make code above asynchronous.

Usually you only may need asyncio when you have multiple I/O operations which you can parallelize with asyncio.gather().

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I guess I am too new to asynchronous operations to have known. This was extremely helpful. The actual operation I need to perform involves calling an external api which makes calculations on a server. I guess I will need to use process pool executor in order to do what I need to. I just need to come up with a way to loop over to put each operation in its own process. Thank you, again
so does it mean we should make this code synchronous rather than asynchronous?
@mic in this case: yes. There's no law that says this code should be synchronous, but making it asynchronous doesn't make sense. You just won't get any benefit out of it. Usually you want to write asynchronous code only when you deal with some I/O or as a way to parallelize some computations (when you can parallelize them). Kindly check the answer from the link, there're few examples there.
0
async def diss(lis):
  x = []
  for i in lis:
    x.append(i + 1)
  return x

async def dat():
  res = await diss(lis)
  return res

loop = asyncio.get_event_loop()

done, _ = loop.run_until_complete(asyncio.wait([dat()]))

for fut in done:
    a = fut.result()

Is what I did to resolve the issue where a is the expected result. Is there anything wrong with this?

1 Comment

It can be written shorter: result = loop.run_until_complete(dat()). You only need asyncio.wait() when awaiting several things in parallel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.