1

I have 4 functions that I wrote in python. I want to make first 3 functions asynchronous. So it should look like this:

x = 3.13
def one(x):
   return x**x

def two(x):
   return x*x*12-314

def three(x):
   return x+x+352+x**x


def final(x):

   one = one(x)
   two = two(x)
   three = three(x)

   return one, two, three

This is what I did:

async def one(x):
   return x**x

async def two(x):
   return x*x*12-314

async def three(x):
   return x+x+352+x**x


def final(x):

   loop = asyncio.get_event_loop()

   one = loop.create_task(one(x))
   two = loop.create_task(two(x))
   three = loop.create_task(three(x))


   #loop.run_until_complete('what should be here')
   #loop.close()

   return one, two, three

But I get this error (if lines above are not commented):

RecursionError: maximum recursion depth exceeded

I do not know what is wrong (Im new to this), I have also tried to add this:

await asyncio.wait([one,two,three])

but to be honest I do not know where and why should I add it. Without that, my code works but it does not give me result, it prints this:

(<Task pending name='Task-1' coro=<one() running at /Users/.../Desktop/....py:63>>, <Task pending name='Task-2' coro=<two() running at /Users/.../Desktop/...py:10>>, <Task pending name='Task-3' coro=<three() running at /Users/.../Desktop/...py:91>>)

Any help?

5
  • 1
    Why are you calling final inside final? Commented Oct 22, 2020 at 9:39
  • whoa, thats my error, but what should I call? I will delete that line Commented Oct 22, 2020 at 9:41
  • Are you aware that making these functions "asynchronous" by declaring them async has no benefit at all? If you are just experimenting to see how to write asynchronous code, following a tutorial might be better than trial-and-error on a self-written toy example. Commented Oct 22, 2020 at 9:49
  • I want to combine these 3 functions in one, but I want to make them asynchronous. I have watched tutorials, but for some reason it does not work like I expected. If you can, please explain me how to fix my problem Commented Oct 22, 2020 at 9:53
  • you need also a sort of long task like io or sleep to demonstrate the async activity Commented Oct 22, 2020 at 10:00

1 Answer 1

2

The major purpose of async syntax and its libraries is to hide the details of event loops. Prefer to use high-level APIs directly:

def final(x):
   loop = asyncio.get_event_loop()
   return loop.run_until_complete(  # run_until_complete fetches the task results
       asyncio.gather(one(x), two(x), three(x))  # gather runs multiple tasks
   )

print(final(x))  # [35.5675357348548, -196.43720000000002, 393.8275357348548]

If you want to explicitly control concurrency, i.e. when functions are launched as tasks, it is simpler to do this inside another async function. In your example, making final an async function simplifies things by giving direct access to await and the ambient loop:

async def final(x):  # async def – we can use await inside
   # create task in whatever loop this function runs in
   task_one = asyncio.create_task(one(x))
   task_two = asyncio.create_task(two(x))
   task_three = asyncio.create_task(three(x))
   # wait for completion and fetch result of each task
   return await task_one, await task_two, await task_three

print(asyncio.run(final(x)))  # (35.5675357348548, -196.43720000000002, 393.8275357348548)
Sign up to request clarification or add additional context in comments.

6 Comments

And should I have loop.close() anywhere? I have seen in some tutorials that they are putting that
I recommend not to explicitly close a loop unless you have to (i.e. you get errors of pending tasks). The purpose of closing a loop is to cancel any tasks that are still queued; ideally, you should not have any queued tasks by the time the loop has done your work. Cargo-culting explicit closing of the loop can accidentally silence errors from tasks that should have finished by themselves but did not.
And what should I do if I get this kind of error: raise TypeError('An asyncio.Future, a coroutine or an awaitable is ' TypeError: An asyncio.Future, a coroutine or an awaitable is required
@taga If this is specifically related to this question/answer, please provide the full traceback. If this is about a new problem/setup, please look for existing questions/answers and consider to ask a new question. I am afraid it is impossible to tell from just that error message.
Its about same question, I get that error in this line : loop.run_until_complete(asyncio.gather(one(x), two(x), three(x))
|

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.