Home > Back-end >  How to use same telethon client with same session many times
How to use same telethon client with same session many times

Time:06-23

I have this Telethon code:

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.

But when using two calls it returns an error : sqlite3.OperationalError: database is locked.

I want to know how to use same client many times without making multiple sessions.

Any help please.

CodePudding user response:

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. the database() is probably locked because the client is already in used.

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?

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

CodePudding user response:

Better you can make a loop of the same session but add a client.disconnect() in the end of main function.

  • Related