【问题标题】:How to use same telethon client with same session many times如何在同一会话中多次使用同一个 telethon 客户端
【发布时间】:2023-01-26 15:41:48
【问题描述】:

我有这个 Telethon 代码:

from telethon import TelegramClient
import asyncio

api_id = ""
api_hash = ""
session = "john"
username = 'Hello_World'   # For Example

async def main():
    client = TelegramClient(session, api_id, api_hash)
    await client.start()

    entity = await client.get_entity("https://t.me/ahsan_alhadeeth")
    search_user = await client.get_participants(entity, search=username)

    print(search_user)

def in_channel():
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(main())

in_channel()
in_channel()

当我对in_channel() 进行一次调用时,它会正常运行直到完成。

但是当使用两个调用时它返回一个错误:sqlite3.OperationalError: database is locked

我想知道如何在不进行多次会话的情况下多次使用同一个客户端。

请任何帮助。

【问题讨论】:

    标签: python asynchronous client python-asyncio telethon


    【解决方案1】:

    我认为不可能在同一时间多次执行相同的任务。由于这是 asyncio 函数,您可能希望在执行下一个 in_channel 之前等待第一个 in_channel() 完成。 database() 可能已锁定,因为客户端已在使用中。

    当您说多次使用同一个客户端时,您的意思是多次运行 in_channel() 直到完成,还是您的目标是同时多次运行 in_channel()?

    我可能会帮助你,但我需要更多答案:-)

    (抱歉,这是一个“答案”。在我得到 50 分之前,我不允许发表评论。)

    【讨论】:

      【解决方案2】:

      更好的是,您可以循环同一会话,但在 main 函数的末尾添加 client.disconnect()

      【讨论】:

      • 您能否也解释一下为什么它更好?这会让你的答案更好,因为它会更容易理解为什么它更好。
      【解决方案3】:

      client = TelegramClient(session, api_id, api_hash)

      TelegramClient 创建与 Telegram 的会话并代表您的机器人或客户端与 Telegram 的通信。只是为了确保,这不是您与任何特定用户的会话(例如:John)。您可以根据需要与任何用户进行交互,前提是有人发起了与您的对话或您的与凭据(api_id、api_hash)关联的用户/机器人被添加到相应的频道/组。 您应该将此客户端视为一个连接,可以在其中创建多个实例来负载平衡多个调用。

      您的代码可以修复如下:

      from telethon import TelegramClient
      import asyncio
      
      class TClient:
          def __init__(self, session):
              api_id = ""
              api_hash = ""
              self.session = session
      
          async def init(self, username = 'Hello_World'):
              self.client = TelegramClient(session, api_id, api_hash)
              await self.client.start()
      
          async def main(self):
              entity = await client.get_entity("https://t.me/ahsan_alhadeeth")
              search_user = await client.get_participants(entity, search=username)
      
              print(search_user)
      
          def in_channel(self, callFn, args):
              loop = asyncio.new_event_loop()
              asyncio.set_event_loop(loop)
              loop.run_until_complete(callFn(args))
      
          in_channel()
      
      if __name__ == '__main__':
          tc1 = TClient(session = "john1")
          tc1.in_channel(tc.init())
          tc1.in_channel(tc.main(username = 'Hello_World'))
          tc1.in_channel(tc.main(username = 'Hello_World2'))
      
          tc2 = TClient(session = "bot2")
          tc2.in_channel(tc.init(username = 'Hello_World'))))
          tc2.in_channel(tc.main(username = 'Hello_World2'))
      

      以上使您能够为多个用户重用您的会话。我对上面的内容做了 2 处修改。

      1. 我重构了代码,以便可以使用具有不同会话 ID 的 Telegram 客户端创建多个连接。
      2. main 函数可以针对不同的用户名多次调用

        PS - 我还没有测试过上面的代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 2017-03-02
        • 1970-01-01
        • 2021-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多