0
import otherFile #non-async functions in there

class A():
    def __init__():
        #do stuff
        self.loop = asyncio.ensure_future(self.function())
    #other async functions

async def function():
    while(True):
        for a in b:
            for x in y:
                result = otherFile.function()###does some requests
                ##do other stuff
        await asyncio.sleep(1*60)

Hey ~ the code from above is a pseudo version of a python program im working/learning on.

As you can see there is a asyncio loop, which iterates over 2(nested) for loops and then does some function calls to a function which is not async, every 60 seconds.

Problem is that if the lists b and y gets larger finishing the loop takes quite the amount of time because of the function call, which does some reuqests and other stuff in it.

My question now is, is there a way so i could do the function call inside the for loops simultaneously to safe some time?

To give some information about the for loops, the size of these lists is not fixed but rather dynamic and depends on some other files

Skijearz
  • 1
  • 1
  • 1
    Take a look at https://stackoverflow.com/questions/34377319/combine-awaitables-like-promise-all You want to use [`asyncio.wait`](https://docs.python.org/3/library/asyncio-task.html#asyncio.wait) or [`asyncio.gather`](https://docs.python.org/3/library/asyncio-task.html#asyncio.gather) – hostingutilities.com Jul 16 '21 at 07:24
  • 1
    I find this `self.loop = asyncio.ensure_future(...)` quite confusing. It creates a task (`create_task` is preferred), not a loop. – VPfB Jul 16 '21 at 08:06

0 Answers0