title: python aiogram how to stop an asynchronous loop
body: <p>Simple example</p>
  <pre><code>import asyncio
  import logging
  from aiogram import Bot, Dispatcher, types
  
  
  logging.basicConfig(level=logging.INFO)
  token = 'token'
  
  bot = Bot(token=token)
  dp = Dispatcher(bot=bot)
  @dp.callback_query_handler(text='stoploop')
  async def stop_loop(query: types.CallbackQuery):
      #  TODO how to stop test loop?
  
      await query.message.edit_text('stop')
  
  
  @dp.callback_query_handler(text='test')
  async def start_loop(query: types.CallbackQuery):
      a = 100
      while True:
          a -= 1
          markup = types.InlineKeyboardMarkup()
          markup.add(types.InlineKeyboardButton('&lt;&lt;&lt;Stop And Back To Home', callback_data='stoploop'))
          await query.message.edit_text(str(a),reply_markup=markup)
          await asyncio.sleep(1)
  
  
  @dp.message_handler(commands='start')
  async def start_cmd_handler(message: types.Message):
      markup = types.InlineKeyboardMarkup()
      markup.add(
          types.InlineKeyboardButton('start loop', callback_data='test')
      )
      await message.reply('test', reply_markup=markup)
  
  
  async def main():
      try:
          await dp.start_polling()
      finally:
          await bot.close()
  
  
  if __name__ == '__main__':
      loop = asyncio.get_event_loop()
      loop.run_until_complete(main())
  </code></pre>
  <p>When I click start_loop, the tg message box on my page starts to display a countdown. When I click stop, how can I stop the previous countdown?
  I use id(query) to confirm that the query instance sent twice is not the same. After I execute the stop_loop function, start_loop will still execute and change the content of the message.
  Can someone tell me how to stop it?</p>