简体   繁体   English

如何使用线程运行具有多个用户帐户的 pyrogram userbot

[英]how to run pyrogram userbot with multipule user accounts using thread

I am coding a userbot in pyrogram but i want to run multiple Clients with different telegram account but i am stuck here.我正在用 pyrogram 编写用户机器人,但我想用不同的电报帐户运行多个客户端,但我被困在这里。 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.我想使用一个脚本在多个帐户上运行 userbot,如果我将单独运行它,那么我必须托管它很多次,我想托管它一次并为我拥有的每个帐户运行。
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: 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'.

With Pyrogram you don't need to use Threading.使用 Pyrogram,您无需使用线程。 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".内部代码已经是完全异步的,你可以一个接一个地启动客户端,然后调用Client.idle()让它们都“活着”。

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 https://gist.github.com/pokurt/96afa69e86725850b2101099461609ed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM