KurimuzonAkuma / pyrogram

Elegant, modern and asynchronous Telegram MTProto API framework in Python for users and bots
https://pyrogram.org
GNU Lesser General Public License v3.0
267 stars 72 forks source link

Added on_startup and on_shutdown for run(), start(), stop() and compose() #36

Open Exponefrv1 opened 3 months ago

Exponefrv1 commented 3 months ago

Note: You can use client methods in on_startup and on_shutdown functions only if you're not specifying a coroutine in run() since new functions run only before coroutine (it means before client is authorized) and after It's finished (after client is terminated).

Example with coroutine specified (you CAN'T use app (client) in new functions):

...
app = Client(...)

async def main():
    async with app:
        me = await app.get_me()
        print(f"App initialized only now, {me.first_name}!")

async def on_startup():
    print("Client is running!")

async def on_shutdown():
    print("Goodbye!")

app.run(main(), on_startup=on_startup, on_shutdown=on_shutdown)

Example without coroutine (you CAN use app (client) in new functions bc app is already running):

...
app = Client(...)

async def on_startup():
    me = await app.get_me()
    print(f"Welcome, {me.first_name}!")

async def on_shutdown():
    me = await app.get_me()
    print(f"Goodbye, {me.first_name}!")

app.run(on_startup=on_startup, on_shutdown=on_shutdown)
zawsq commented 3 months ago

This make your code much more confusing, it's just something you can implement without it being added in the fork source code.

It's always best to manually do task during start() as stop().

You should propose a clear use case and benefits of this pr.

Exponefrv1 commented 3 months ago

This make your code much more confusing, it's just something you can implement without it being added in the fork source code.

It's always best to manually do task during start() as stop().

You should propose a clear use case and benefits of this pr.

Probably you're right, but it's just more convenient. Instead of 4 lines of code you write only 1. These args are optional so why not?

I was inspired by this enhancement: https://github.com/pyrogram/pyrogram/issues/1264