简体   繁体   English

如何多次使用具有相同 session 的同一个电视节目客户端

[英]How to use same telethon client with same session many times

I have this Telethon code:我有这个 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()

when I use a single call to in_channel() it runs normally until finish.当我对in_channel()进行一次调用时,它会正常运行直到完成。

But when using two calls it returns an error: sqlite3.OperationalError: database is locked .但是当使用两个调用时它返回一个错误: sqlite3.OperationalError: database is locked

I want to know how to use same client many times without making multiple sessions.我想知道如何在不进行多次会话的情况下多次使用同一个客户端。

Any help please.请任何帮助。

I dont think its possible to do the same task multiple times, at the SAME time.我认为不可能在同一时间多次执行相同的任务。 Since this is asyncio functions, you would want to wait on the first in_channel() to finish, before the next in_channel is executed.由于这是 asyncio 函数,您可能希望等待第一个 in_channel() 完成,然后再执行下一个 in_channel。 the database() is probably locked because the client is already in used. database() 可能已被锁定,因为客户端已在使用中。

When you say use the same client many times, do you mean, run in_channel() multiple times untill it is complete, or is your goal to run in_channel() many times simultaniously?当您说多次使用同一个客户端时,您的意思是多次运行 in_channel() 直到它完成,还是您的目标是同时运行多次 in_channel()?

I could probably help you, but i would need more answers :-)我可能会帮助你,但我需要更多答案:-)

(sorry this is a 'Answer'. im not allowed to make a comment untill i have 50 points.) (对不起,这是一个“答案”。在我得到 50 分之前,我不允许发表评论。)

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

client = TelegramClient(session, api_id, api_hash)

TelegramClient creates a session with Telegram and represents your bot or client communication with Telegram. TelegramClient 使用 Telegram 创建一个 session 并代表您的机器人或客户端与 Telegram 的通信。 Just to ensure, this is not your session with any particular user ( for ex: John).只是为了确保,这不是您与任何特定用户(例如:约翰)的 session。 You can interact with any no of users, you want, subject to someone having initiated a conversation with you or your user/bot associated with credentials (api_id, api_hash) being added to the respective channel/ group.您可以根据需要与任何用户进行交互,前提是有人发起了与您的对话或您的与凭据(api_id、api_hash)关联的用户/机器人被添加到相应的频道/组。 You should think of this client as one connection where multiple instances can be created to load balance multiple calls.您应该将此客户端视为一个连接,可以在其中创建多个实例来负载平衡多个调用。

Your code can be fixed as below:您的代码可以修复如下:

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'))

The above enables you to reuse your session for multiple users.以上内容使您能够为多个用户重复使用 session。 I made 2 modifications to the above.我对上面的内容做了 2 处修改。

  1. I have refactored the code so that multiple connections can be created with Telegram clients with different session ids.我重构了代码,以便可以使用具有不同 session id 的 Telegram 客户端创建多个连接。
  2. main function can be call multiple times for various usernames主 function 可以多次调用各种用户名

PS - I haven't tested the above code. PS - 我还没有测试过上面的代码。

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

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