# Running multiple asyncio coroutines together

**URL:** <https://discuss.python.org/t/running-multiple-asyncio-coroutines-together/55026>\
**Category:** Python Help\
**Created:** [June 5, 2024, 2:54pm UTC](https://discuss.python.org/t/running-multiple-asyncio-coroutines-together/55026 "2024-06-05T14:54:33Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![elis.byberi](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/elis.byberi/32/35937_2.png) [@elis.byberi](https://discuss.python.org/u/elis.byberi)\
**Post date:** [June 6, 2024, 1:38pm UTC](https://discuss.python.org/t/running-multiple-asyncio-coroutines-together/55026/5 "2024-06-06T13:38:29Z")

</div>

I would run each client in separate threads.  
Here is a similar example to: [discord.py · PyPI](https://pypi.org/project/discord.py/) - [Quickstart](https://discordpy.readthedocs.io/en/latest/quickstart.html)

```python
import asyncio
import threading
import time

class DummyClient:
    def __init__ (self):
        self.loop = asyncio.new_event_loop()
        self.user = 'DummyUser'
    
    async def on_ready(self):
        print(f'We have logged in as {self.user}')
    
    async def on_message(self, message):
        if message.author == self.user:
            return
        
        if message.content.startswith('$hello'):
            await self.send_message('Hello!')
    
    async def send_message(self, content):
        print(f'Message sent: {content}')
    
    async def simulate_message(self, message):
        await self.on_message(message)
    
    async def print_periodically(self):
        while True:
            print('Print from DummyClient')
            await asyncio.sleep(2.0) # Print every 2 seconds
    
    def run(self):
        asyncio.set_event_loop(self.loop)
        self.loop.run_until_complete(self.on_ready())
        
        # Simulate receiving messages
        self.loop.create_task(self.simulate_message(DummyMessage('User1', '$hello')))
        self.loop.create_task(self.simulate_message(DummyMessage('DummyUser', '$hello')))
        
        # Start printing periodically
        self.loop.create_task(self.print_periodically())
        
        # Keep the loop running
        self.loop.run_forever()

class DummyMessage:
    def __init__ (self, author, content):
        self.author = author
        self.content = content

def run_dummy_client():
    client = DummyClient()
    client.run()

# Create a thread to run the dummy client
client_thread = threading.Thread(target=run_dummy_client)

# Start the thread
client_thread.start()

# Now you can continue with other tasks in the main thread
print("Dummy async client is running in a separate thread.")

# This loop runs in the main thread
while True:
    time.sleep(1.0)
    print('Hi!')

```

Result:

```python
Dummy async client is running in a separate thread.
We have logged in as DummyUser
Message sent: Hello!
Print from DummyClient
Hi!
Hi!
Print from DummyClient
Hi!
Hi!
...

```

---

_[View the full topic](https://discuss.python.org/t/running-multiple-asyncio-coroutines-together/55026)._
