0
from pyrogram import Client

app = Client(client_name, api_id=api_id, api_hash=api_hash)

async def handle_messages(client, message):
    await some_other_function()

app.run() 

If multiple messages are received together, it seems to process them one by one. How can I process multiple messages in parallel?

understack
  • 11,212
  • 24
  • 77
  • 100

1 Answers1

0

If some_other_function() is CPU intensive and not I/O intensive then to get true parallelism you need to implement multiprocessing or multithreading that being said the issue here might only be I/O wait time so you may still handle multiple messages pretty efficiently using asynchronous code only. instead of awaiting the some_other_function. you just create a task and just let it run. asynchronous co routines start executing immediately after task is created. so use

asyncio.create_task(some_other_function(message))
Sandeep Polamuri
  • 609
  • 4
  • 10