abraguez
abraguez

Reputation: 375

How to poll with aiogram without the executor?

I am trying to use aiogram, but as a component of a larger application, most examples do the following call in the main after defining the callbacks>

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

What I am trying to implement is having the polling in a separate task which I can run concurrently with asyncio.gather() as shown below:

import asyncio

async def task1():
    while True:
        # Perform aiogram polling
        await asyncio.sleep()

async def task2():
    while True:
        # Perform other application's component
        await asyncio.sleep()

async def main():
    await asyncio.gather(
        task1(),
        task2(),    
    )


if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(main())

I searched in the code for the executor class, yet it performs an startup sequence and then calls itself the loop.get_event_loop() and loop.run_forever(),

Does aiogram have a built-in method for performing this?

Upvotes: 0

Views: 3348

Answers (1)

abraguez
abraguez

Reputation: 375

The solution was to use the dispatcher directly rather than the executor utility as shown below:

async def task1():
    await dp.start_polling()

async def task2():
    while True:
        # Perform other application's component
        await asyncio.sleep(1)

async def main():
    await asyncio.gather(
        task1(),
        task2(),    
    )

if __name__ == '__main__':
    asyncio.get_event_loop().run_until_complete(main())

Upvotes: 3

Related Questions