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

adam
  • 47
  • 1
  • 5
  • 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`](https://docs.python.org/3/library/asyncio-task.html#asyncio.run) – Patrick Haugh Sep 10 '19 at 19:21
  • 1
    as per [https://stackoverflow.com/questions/52796630/python3-6-attributeerror-module-asyncio-has-no-attribute-run] I found that python 3.6 asyncio does not support run – adam Sep 10 '19 at 19:36

2 Answers2

1

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().

Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
  • 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 – adam Sep 12 '19 at 14:22
  • so does it mean we should make this code synchronous rather than asynchronous? – mic Dec 31 '21 at 04:23
  • @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](https://stackoverflow.com/a/33399896/1113207) from the link, there're few examples there. – Mikhail Gerasimov Dec 31 '21 at 14:44
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?

adam
  • 47
  • 1
  • 5
  • It can be written shorter: `result = loop.run_until_complete(dat())`. You only need asyncio.wait() when awaiting several things in parallel. – user4815162342 Sep 11 '19 at 06:00