2

I am coding a userbot in pyrogram but I want to run multiple Clients with different telegram account but I am stuck here. I want to run userbot on multiple accounts using one script if I will run it separately then I have to host it so many time I want to host it for one time and run for every account I have.
I think this will help to understand what I am saying.


from pyrogram import Client, filters, handlers, idle
import threading
from pyrogram.handlers import MessageHandler

app1 = Client(
    session, api_hash, api_id)


app2 = Client(session,
              api_hash, api_id)


accounts = [app1, app2]


async def handlngmessage(client, message):
    print(message)
    print("\nstarted ")
    await client.send_message("me", "recived")


def runner(c):
    c.run()


for ac in accounts:
    ac.add_handler(handlers.MessageHandler(unmutedtest))
    t = threading.Thread(target=runner, args=(ac,))
    t.start()
    t.join()

When I run this I am just getting error

Output:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ak/Desktop/development/bots/pyrogramplugins/userbot/main.py", line 30, in runner
    c.run()
  File "/home/ak/.local/lib/python3.9/site-packages/pyrogram/methods/utilities/run.py", line 50, in run
    loop = asyncio.get_event_loop()
  File "/usr/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-1'.
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 954, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "/home/ak/Desktop/development/bots/pyrogramplugins/userbot/main.py", line 30, in runner
    c.run()
  File "/home/ak/.local/lib/python3.9/site-packages/pyrogram/methods/utilities/run.py", line 50, in run
    loop = asyncio.get_event_loop()
  File "/usr/lib/python3.9/asyncio/events.py", line 642, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'Thread-2'.
Shivang Kakkar
  • 421
  • 3
  • 15
Akrash Nadeem
  • 96
  • 2
  • 11

1 Answers1

2

With Pyrogram you don't need to use Threading. The internal code is already entirely asynchronous and you can just start the clients one after another, then call Client.idle() to keep them all "alive".

from pyrogram import Client

app1 = Client("first account")
app2 = Client("second account")

# You can either stack decorators ...
@app1.on_message()
@app2.on_message()
async def m_func(_, message):
    pass

# ... or use multiple add_handler calls
app1.add_handler(MessageHandler(m_func))
app2.add_handler(MessageHandler(m_func))

# Then start all Clients and call idle() to keep them running
app1.start()
app2.start()
Client.idle()
app1.stop()
app2.stop()

Alternatively, here's a Gist with some more explanation.
https://gist.github.com/pokurt/96afa69e86725850b2101099461609ed

ColinShark
  • 1,047
  • 2
  • 10
  • 18
  • I Am using almost 50 clients so it take a lot of time to start every account.Is there any way to run them parallel which will reduce time and the program will start in less time – Akrash Nadeem Apr 09 '22 at 14:24
  • @user14195337 I guess there you can use Threading. Start like 5 Threads with 10 accounts each. To solve 'no current event loop' exception, in every new thread use asyncio.set_event_loop(asyncio.new_event_loop()) to set event loop for each thread. – GaussGun Jul 21 '22 at 15:26