5

Assuming, that I have a class like that:

class MyClass:
    def __init__(self):
        run_some_long_time_function()

How can I create many instances of this class in parallel using asyncio in python 3.4.1?

roboslone
  • 2,847
  • 3
  • 20
  • 28
  • related: [get output from *bash* commands concurrently using `asyncio`](http://stackoverflow.com/a/23616229/4279) – jfs Jul 30 '14 at 02:46

1 Answers1

7

The asyncio event loop is single threaded, so nothing running on the event loop will run in parallel. You can however spawn a thread and wait for it to finish. The default executor should create a thread for you:

loop = asyncio.get_event_loop()

asyncio.async(loop.run_in_executor(None, lambda: MyClass()))
asyncio.async(loop.run_in_executor(None, lambda: MyClass()))
leech
  • 8,293
  • 7
  • 62
  • 78