New to Pyrogram? Take a look here.

parvat_r

Parvat R

Posted on August 12, 2021

New to Pyrogram? Take a look here.

Define a client in different way:

/main.py:

import pyrogram

client = pyrogram.Client("Session-Name")
Enter fullscreen mode Exit fullscreen mode

/config.ini:

[pyrogram]
api_id=1234567
api_HASH=1108f129f7afa345076448421e535bbd
bot_token=1234567890:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Enter fullscreen mode Exit fullscreen mode

You can get your api_id, api_hash from telegram. And, you can get the token from @botfather


Do long time process, without disturbing the bot

import _thread

def some_long_process(param1, param2):
    # do something that takes long time
    param2.reply_text('Process Over.')
    ...


@client.on_message(
    pyrogram.filters.command('do_big')
)
def do_big(_c, message):
    _thread.start_new_thread(some_long_process, (_c, message))
Enter fullscreen mode Exit fullscreen mode

You can pass on the client and the message parameter to some function, which takes a long time to complete, and has to notify the user when it completes. During this process, some times the bot might stop responding to other users, or become slow.

So, use _thread module to start a new thread which runs in background without disturbing your bot, and making it faster to the core.

💖 💪 🙅 🚩
parvat_r
Parvat R

Posted on August 12, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

New to Pyrogram? Take a look here.
python New to Pyrogram? Take a look here.

August 12, 2021