I am new to Python and trying to initialize the Telethon Client on start of a Flask app like
def telegram_initailize():
global client
global loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
client = TelegramClient(
session=MOBILE, api_id=int(API_ID), api_hash=API_HASH, loop=loop
)
And then when a request comes trying to use it as
def run_when_request_come():
global client
with client:
# logic here
But it errors out to RuntimeError: There is no current event loop in thread 'Thread-2 (process_request_thread)
Shifting the code of telegram_initialize
inside run_when_request_come
works well but it will initialize the client and loop everytime the function is executed