10

TL;DR: I want to create asyncio task/coroutine and get the return values to be assigned in a var.

-

I found this question Getting values from functions that run as asyncio tasks Which seems to talk about a similar issue, but the sintax has changed a lot in the asyncio module that I not even sure if it's related. [I'm on Python 3.7.2]

Sample code for me to explain what I'm trying to do:

async def s(f):
    func = {
        1: op1,
        2: op2,
        3: op3
      }.get(f,False)
    var = func(f)
    return var

def op1(f):
    print('Op1',f+f)
    return f+f

def op2(f):
    print('Op2', f*f)
    return f*f

def op3(f):
    print('Op3', f**f)
    return f**f

async def main():
    task1 = asyncio.create_task(s(1))
    task2 = asyncio.create_task(s(3))
    print(f"started at {time.strftime('%X')}")
    await task1
    await task2
    print(task1,task2)
    print(f"finished at {time.strftime('%X')}")

The main() is a example coroutine from docs https://docs.python.org/3/library/asyncio-task.html#coroutines

The s() is supposed to be a switch which will select the proper function (one of op1,op2,op3) for the scenario, run the function and RETURN the result, (from what I expected) to be assigned to task1/task2.

What is actually assigned to the task1/task2 is:

<Task finished coro=<s() done, defined at C:\...\test.py:17> result=2> 
<Task finished coro=<s() done, defined at C:\...\test.py:17> result=27>

As you can see, the return is stored in 'result' (attribute? var?) but not directly assigned to the var.

I need either: Assign the return directly to the var OR a way to access the 'result' and assign it to a var where I can further manipulate.

BTW: print(task1.result) returns this: <built-in method result of _asyncio.Task object at 0x0000028592DDA048>

1
  • 1
    Sounds like you need to write: print(task1.result()). Commented Apr 21, 2019 at 1:58

1 Answer 1

13
async def main():
    task1 = asyncio.create_task(s(1))
    task2 = asyncio.create_task(s(3))
    print(f"started at {time.strftime('%X')}")
    result_of_task1 = await task1
    result_of_task2 = await task2
    print(result_of_task1,result_of_task2)
    print(f"finished at {time.strftime('%X')}")

is one way to do it. I prefer this, as it makes awaiting tasks more like normal function calls, but also equivalent is

await task1
# some time later
result_of_task1 = task1.result()

You just missed the (), because result is a method, not a member variable.

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

3 Comments

I was under the impression you couldn't assign await expression. Anyway, both ways solved my issue. Thanks!
How can I change the above such that function s can return multiple values?
@AnuragAS you can simply return a tuple and receive it as a tuple, just like any other python function

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.