1

I have a code similar to this, and I want the function "do_something" to be executed when I run the project. Its job is to prepare the database, etc.

I don't know how do it. I would it if you could guide me. I have read the rocketry documentation, but I couldn't find what I need.

from pyrogram import Client
from rocketry import Rocketry
import asyncio

api = Client("api")
cli = Client("cli")
rocktry = Rocketry()

async def do_something():
    await api.send_message(123456, "loading ....")
    # code
    await api.send_message(123456, "bot is ready!")

async def main():
    await asyncio.gather(api.start(), cli.start(), rocketry.serve())

api.run(main())

The idea that came to my mind is to set the value of a variable to false and then check in my code if the variable's value is false, perform this action, and afterwards set the value of that variable to true.

Mikael Öhman
  • 2,294
  • 15
  • 21
sobhani
  • 23
  • 5

2 Answers2

1
async def main():
    await asyncio.gather(api.start(), cli.start(), rocktry.serve())
    await do_something()

api.loop.run_until_complete(main())

This will run the do_something() function after the clients have started and the Rocketry server has started.

nischal sharma
  • 479
  • 1
  • 14
1

I change my code to this , and it's worked correctly:

from pyrogram import Client
from rocketry import Rocketry
import asyncio

api = Client("api")
cli = Client("cli")
rocktry = Rocketry()

async def do_something():
    while not api_app.is_connected:
        await asyncio.sleep(1)

    await api.send_message(123456, "loading ....")
    # code
    await api.send_message(123456, "bot is ready!")

async def main():
    await asyncio.gather(
        api_app.start(),
        do_something(),
        cli_app.start(),
        skd_app.serve()
    )

api.run(main())

I don't know if it has good performance or not, but it's exactly what I was looking for.

sobhani
  • 23
  • 5