superdee73
superdee73

Reputation: 403

How do I use async to loop over a list and call a list object's own function

How can I loop over a list of objects async and call their function. For example:

class Cat:
    def talk():
        print("Meow")

class Dog:
    def talk():
        print("Woof")


cat = Cat()
dog = Dog()

animal_list = [cat, dog]

# How would I do these async?
for animal in animal_list:
    animal.talk()

This thread, How to use an async for loop to iterate over a list?, recommends using asyncio, but doesn't example how I can have an object call it's own function such as animal.talk()

Upvotes: 3

Views: 5013

Answers (2)

ross-spencer
ross-spencer

Reputation: 11

I like @Hurried-Helpful's post on this. Making their example work might look as follows.

import asyncio
import random
import time

class Cat:
    @staticmethod
    async def talk():
        await asyncio.sleep(random.choice([1,2]))
        print("Meow")

class Dog:
    @staticmethod
    async def talk():
        await asyncio.sleep(random.choice([0,1]))
        print("Woof")

async def main():
    print(f"Started at: {time.strftime('%X')}")
    cat = Cat()
    dog = Dog()
    animal_list = [cat, dog]
    res = await asyncio.gather(
        *[animal.talk() for animal in animal_list]
    )
    print(f"Ended at: {time.strftime('%X')}")

asyncio.run(main())

The Python docs are much better on async functions now, and also contain good examples: https://docs.python.org/3/library/asyncio-task.html#running-tasks-concurrently

Upvotes: 1

Hurried-Helpful
Hurried-Helpful

Reputation: 2000

Make the talk functions async otherwise there's no point using asyncio.

class Cat:
    async def talk():
        print("Meow")

class Dog:
    async def talk():
        print("Woof")


cat = Cat()
dog = Dog()

animal_list = [cat, dog]

Create a list (Iterable) of coroutines returned by animal.talk().

Either coroutines = map(lambda animal : animal.talk(), animal_list) or coroutines = [animal.talk() for animal in animal_list] will do.

Then finally scheduled the list of coroutines for execution.

# This returns the results of the async functions together.
results = await asyncio.gather(coroutines)

# This returns the results one by one.
for future in asyncio.as_completed(coroutines):
    result = await future

cat.talk() and dog.talk() will be executed asynchronously, which means the order of their execution is not guaranteed, and may be run on different threads. But here the talk function is so simple that it will look like it's run synchronously, and confers no real benefits.

But if talk involved making a network request or a long, heavy computation, and the animal_list were very long, doing it this way can help with the performance.

Upvotes: 1

Related Questions