@utsiye

Pyrogram в многопотоке ругается, почему?

Вот мой код:
def auth_app(number_app,api_id,api_hash):
    client = Client(f'apps/{number_app}', api_id=api_id, api_hash=api_hash)
    client.start()
    client.stop()

def create_app(app_name):
    client=Client(f'apps/{app_name}')
    #КОД
    client.run()

accounts=[['api id', 'api hash']]
for i in range(len(accounts)):
    if len(accounts[i])==2:
        auth_app(i + 1, accounts[i][0], accounts[i][1])
for i in os.listdir('apps'):
    threading.Thread(target=lambda:create_app(i)).start()


Выдает такую ошибку:
Exception in thread Thread-1 (<lambda>):
Traceback (most recent call last):
  File "C:\Users\gelse\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "C:\Users\gelse\AppData\Local\Programs\Python\Python310\lib\threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\gelse\PycharmProjectsd\pyrogram_chatgpt\main.py", line 67, in <lambda>
    threading.Thread(target=lambda:create_app(i)).start()
  File "C:\Users\gelse\PycharmProjectsd\pyrogram_chatgpt\main.py", line 31, in create_app
    client=Client(f'apps/{app_name}')
  File "C:\Users\gelse\PycharmProjectsd\pyrogram_chatgpt\venv\lib\site-packages\pyrogram\client.py", line 267, in __init__
    self.dispatcher = Dispatcher(self)
  File "C:\Users\gelse\PycharmProjectsd\pyrogram_chatgpt\venv\lib\site-packages\pyrogram\dispatcher.py", line 58, in __init__
    self.loop = asyncio.get_event_loop()
  File "C:\Users\gelse\AppData\Local\Programs\Python\Python310\lib\asyncio\events.py", line 656, 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 (<lambda>)'.


Первый цикл, проверяет аккаунты и запрашивает данные для входа, если они не авторизированы, второй цикл, запускает аккаунты.
В чем проблема? Что не так?
  • Вопрос задан
  • 313 просмотров
Решения вопроса 1
@utsiye Автор вопроса
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()
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы