title: Cannot close a running event loop
body: <p>What is the way to close the discord.py bot loop once tasks are done?</p>
  <p>Added:</p>
  <pre><code>nest_asyncio.apply()
  </code></pre>
  <p>Tried:</p>
  <pre><code>bot.logout(), bot.close(), bot.cancel()
  </code></pre>
  <p>Code:</p>
  <pre><code>async def helper(bot, discord_user_feed):
      nest_asyncio.apply()
      await bot.wait_until_ready()
      # await asyncio.sleep(10)
      for each_id, events_list in discord_user_feed.items():
          discord_user = await bot.fetch_user(int(each_id))
          for each_one in events_list:
              msg_sent = True
              while msg_sent:
                  await discord_user.send(each_one)
                  msg_sent = False
      await bot.close()
      return 'discord messages sent'
  
  
  async def discord_headlines(request):
      nest_asyncio.apply()
      discord_user_feed =  await get_details()
      bot = commands.Bot(command_prefix='!')
      bot.loop.create_task(helper(bot, discord_user_feed))
      message = bot.run('my_token')
      return HttpResponse(message)
  </code></pre>
  <p>I can be able to send messages to discord users using id and discord bot. But, even after, django view api is continuously running and getting the error. It needs to return a response message - discord messages sent.</p>
  <p>Error:</p>
  <pre><code>Exception in callback &lt;TaskWakeupMethWrapper object at 0x000001C852D2DA38&gt;(&lt;Future finis...SWdZVtXT7E']}&gt;)
  handle: &lt;Handle &lt;TaskWakeupMethWrapper object at 0x000001C852D2DA38&gt;(&lt;Future finis...SWdZVtXT7E']}&gt;)&gt;
  Traceback (most recent call last):
    File &quot;E:\sd\envs\port\lib\asyncio\events.py&quot;, line 145, in _run
      self._callback(*self._args)
  KeyError: &lt;_WindowsSelectorEventLoop running=True closed=False debug=False&gt;
  Internal Server Error: /api/v0/discord_headlines/
  Traceback (most recent call last):
    File &quot;E:\sd\envs\port\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;E:\sd\envs\port\lib\site-packages\django\core\handlers\base.py&quot;, line 179, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;E:\sd\envs\port\lib\site-packages\asgiref\sync.py&quot;, line 139, in __call__
      return call_result.result()
    File &quot;E:\sd\envs\port\lib\concurrent\futures\_base.py&quot;, line 425, in result
      return self.__get_result()
    File &quot;E:\sd\envs\port\lib\concurrent\futures\_base.py&quot;, line 384, in __get_result
      raise self._exception
    File &quot;E:\sd\envs\port\lib\site-packages\asgiref\sync.py&quot;, line 204, in main_wrap
      result = await self.awaitable(*args, **kwargs)
    File &quot;E:\back-end\port_api\views\discord_integration.py&quot;, line 110, in discord_headlines
      message = bot.run('my_token')
    File &quot;E:\sd\envs\port\lib\site-packages\discord\client.py&quot;, line 719, in run
      _cleanup_loop(loop)
    File &quot;E:\sd\envs\port\lib\site-packages\discord\client.py&quot;, line 95, in _cleanup_loop
      loop.close()
    File &quot;E:\sd\envs\port\lib\asyncio\selector_events.py&quot;, line 107, in close
      raise RuntimeError(&quot;Cannot close a running event loop&quot;)
  RuntimeError: Cannot close a running event loop
  [13/Jun/2021 01:07:34] &quot;GET /api/v0/discord_headlines/ HTTP/1.1&quot; 500 109010
  </code></pre>
  <p>These are the versions:</p>
  <pre><code>Python 3.6.4, Django 3.1, discord.py 1.7.3, Asyncio, Anaconda-4.9.2, Windows-10
  </code></pre>