Answer the question
In order to leave comments, you need to log in
How to run a parallel loop in aiogram bot?
There is a bot in which you need 2 processes in parallel, the bot pulling itself and another cycle.
The 2 processes themselves work, but the problem is that messages are not sent in the cycle due to async functions, I can’t figure out how to do it, here is the code:
The function itself:
def is_enabled():
while True:
for user_id in users:
send_message(user_id)
time.sleep(15)
async def send_message(user_id=None):
if user_id:
await bot.send_message(chat_id=user_id, text='123')
if __name__ == '__main__':
Process(target=is_enabled).start()
Process(target=executor.start_polling(dp, skip_updates=True)).start()
Answer the question
In order to leave comments, you need to log in
made such a code, but the bot itself does not work, but the loop works...
UPD: the working version is below in the comment
async def is_enabled():
print('запускаю цикл')
while True:
print('отправляю сообщения')
for user_id in users:
await bot.send_message(chat_id=user_id, text='123')
print('отправил')
print('жду')
time.sleep(10)
async def on_startup(x):
asyncio.create_task(is_enabled())
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True, on_startup=on_startup)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question