From 7040cfb0dce7d238f8822a53c5c46fb784803ed0 Mon Sep 17 00:00:00 2001 From: damienkilgannon Date: Wed, 14 Nov 2018 16:01:44 +0000 Subject: [PATCH] updated docs --- .../basic/compatibility-and-convenience.rst | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/readthedocs/extra/basic/compatibility-and-convenience.rst b/readthedocs/extra/basic/compatibility-and-convenience.rst index 86be8c1c..84ab9040 100644 --- a/readthedocs/extra/basic/compatibility-and-convenience.rst +++ b/readthedocs/extra/basic/compatibility-and-convenience.rst @@ -185,3 +185,27 @@ that, you need to use ``asyncio`` correctly too. For this reason, there is a section called :ref:`mastering-asyncio` that will introduce you to the ``asyncio`` world, with links to more resources for learning how to use it. Feel free to check that section out once you have read the rest. + +Using Telethon with Flask +************************* + +When using Telethon in a Flask app Telethon will try to access the event +loop in the main program thread if an event loop is not explicitly +passed to the TelegramClient object. When using Flask with auto reloader +active then it does not run in the main thread and Telethon will fail +to find the active event loop. To solve this either disable the +use_reloader via; + +.. code-block:: python + + app = Flask(__name__) + app.run(host=localhost, use_reloader=False) + +or explicitly pass a new event loop to the TelegramClient in the running +thread; + +.. code-block:: python + + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + tg_client = TelegramClient(PATH, TELEGRAM_API_ID, TELEGRAM_API_HASH, loop=loop)