title: Restart asyncio loop in exception?
body: <p>I want each time a exception is thrown like 502 HTTP  error or Floodwaiterror to restart the loop or continue but i dont know or figure out how to do it </p>
  
  <p>Code:</p>
  
  <pre><code>import time
  from telethon import TelegramClient,errors
  from telethon.tl.functions.photos import UploadProfilePhotoRequest
  import os
  import urllib.request
  import urllib.error
  import asyncio
  from datetime import datetime
  import datetime
  api_id =  XXXXXX
  api_hash = 'XXXXXXXXXXXXXXXX'
  
  client = TelegramClient('Test_Session', api_id, api_hash)
  client.start()
  
  async def Change_Photo():
      try:
          while True:
              time.sleep(3)
              if os.path.exists("cat.jpg"):
                  os.remove("cat.jpg")
              if os.path.exists("cat"):
                  os.remove("cat")
              urllib.request.urlretrieve("https://cataas.com/cat", "cat")
              os.rename('cat', 'cat.jpg')
  
              await client(UploadProfilePhotoRequest(await client.upload_file(r"C:\Users\localhost\PycharmProjects\Scripts Python\Telethon\cat.jpg")))
              now = datetime.datetime.now()
              os.remove("cat.jpg")
              print("Changed Photo:", now.strftime("%Y-%m-%d %H:%M:%S"))
      except errors.FloodWaitError as e:
          print('Have to sleep', e.seconds, 'seconds')
          await asyncio.sleep(e.seconds)
  
      except urllib.error.HTTPError as e:
          if e.code == 502:
              print("HTTP error 502... Trying again")
  
  
  
  
  
  loop = asyncio.get_event_loop()
  loop.run_until_complete(Change_Photo())
  loop.close()
  </code></pre>
  
  <p>Output of exception witouth closing the loop in each exception</p>
  
  <pre><code>Changed Photo: 2019-05-03 14:48:40
  Have to sleep 495 seconds
  Task was destroyed but it is pending!
  task: &lt;Task pending coro=&lt;UpdateMethods._dispatch_update() running at C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\client\updates.py:252&gt;&gt;
  C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py:609: RuntimeWarning: coroutine 'UpdateMethods._dispatch_update' was never awaited
    self._ready.clear()
  RuntimeWarning: Enable tracemalloc to get the object allocation traceback
  Task was destroyed but it is pending!
  task: &lt;Task pending coro=&lt;Connection._send_loop() running at C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\connection\connection.py:131&gt; wait_for=&lt;Future pending cb=[&lt;TaskWakeupMethWrapper object at 0x000002BFDB114DC8&gt;()]&gt;&gt;
  Task was destroyed but it is pending!
  task: &lt;Task pending coro=&lt;Connection._recv_loop() running at C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\connection\connection.py:149&gt; wait_for=&lt;Future pending cb=[&lt;TaskWakeupMethWrapper object at 0x000002BFDB06E318&gt;()]&gt;&gt;
  Task was destroyed but it is pending!
  task: &lt;Task pending coro=&lt;MTProtoSender._send_loop() running at C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\mtprotosender.py:378&gt; wait_for=&lt;Future pending cb=[&lt;TaskWakeupMethWrapper object at 0x000002BFDB114A68&gt;()]&gt;&gt;
  Task was destroyed but it is pending!
  task: &lt;Task pending coro=&lt;MTProtoSender._recv_loop() running at C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\mtprotosender.py:416&gt; wait_for=&lt;Future pending cb=[&lt;TaskWakeupMethWrapper object at 0x000002BFDB114C48&gt;()]&gt;&gt;
  Task was destroyed but it is pending!
  task: &lt;Task pending coro=&lt;UpdateMethods._update_loop() running at C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\client\updates.py:213&gt; wait_for=&lt;Future pending cb=[&lt;TaskWakeupMethWrapper object at 0x000002BFDB114EE8&gt;()]&gt;&gt;
  Exception ignored in: &lt;coroutine object Connection._send_loop at 0x000002BFDB0F48C8&gt;
  Traceback (most recent call last):
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\connection\connection.py", line 141, in _send_loop
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\connection\connection.py", line 96, in disconnect
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 688, in call_soon
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 480, in _check_closed
  RuntimeError: Event loop is closed
  Exception ignored in: &lt;coroutine object MTProtoSender._recv_loop at 0x000002BFDB0F4BC8&gt;
  Traceback (most recent call last):
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\mtprotosender.py", line 416, in _recv_loop
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\network\connection\connection.py", line 119, in recv
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\queues.py", line 161, in get
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 688, in call_soon
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 480, in _check_closed
  RuntimeError: Event loop is closed
  </code></pre>
  
  <p>I also try closing  the loop in each exception (loop.close)</p>
  
  <pre><code>except errors.FloodWaitError as e:
      print('Have to sleep', e.seconds, 'seconds')
      await asyncio.sleep(e.seconds)
  
  except urllib.error.HTTPError as e:
      if e.code == 502:
          print("HTTP error 502... Trying again")
  </code></pre>
  
  <p>But either worked:</p>
  
  <pre><code> Have to sleep 761 seconds
  Traceback (most recent call last):
    File "C:/Users/localhost/PycharmProjects/Scripts Python/Telethon/anykipsa.py", line 27, in Change_Photo
      await client(UploadProfilePhotoRequest(await client.upload_file(r"C:\Users\localhost\PycharmProjects\Scripts Python\Telethon\cat.jpg")))
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\site-packages\telethon\client\users.py", line 60, in __call__
      result = await future
  telethon.errors.rpcerrorlist.FloodWaitError: A wait of 761 seconds is required (caused by UploadProfilePhotoRequest)
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "C:/Users/localhost/PycharmProjects/Scripts Python/Telethon/anykipsa.py", line 45, in &lt;module&gt;
      loop.run_until_complete(Change_Photo())
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\base_events.py", line 584, in run_until_complete
      return future.result()
    File "C:/Users/localhost/PycharmProjects/Scripts Python/Telethon/anykipsa.py", line 34, in Change_Photo
      loop.close()
    File "C:\Users\localhost\AppData\Local\Programs\Python\Python37\lib\asyncio\selector_events.py", line 83, in close
      raise RuntimeError("Cannot close a running event loop")
  RuntimeError: Cannot close a running event loop
  
  Process finished with exit code 1
  </code></pre>
  
  <p>Im pretty new to coding learned python some weeks ago for sure im doing something bad but i cannot find what</p>