asyncio not working on Google Cloud Functions

I have this function which works fine locally on my machine with python 3.8, but it throws runtime error on Google Cloud Functions.

def telegram_test(request):
    request_json = request.get_json()
    import datetime
    import pandas as pd
    from pyrogram import Client
    session_string = "...............38Q8uTHG5gHwyWD8nW6h................."
    # the rest of the authantication
    api_id = 32494131641215
    api_hash = "ioadsfsjnjksfgnfriuthg#qw]/zwq  ]w/lc ec,"

    # one of bbc channels on telegram you want to access
    channel_name = 'pyrogram'

    # if you only want to get messages older than 7 days in unix style
    seven_days = int(( - datetime.timedelta(days=7)).timestamp())
    # call telegram with parameters such as limit and date
    # save the result to dataframe

    with Client(session_string,api_id,api_hash, takeout=True,workers=2) as app:
        hist_iter = app.iter_history(channel_name,offset_date=seven_days, limit=100)    
        msglist = [msg.__dict__ for msg in hist_iter]
        df = pd.DataFrame(msglist)
    return f'it works!:{request_json}'

The error message I get from GCF log:

File “/opt/python3.8/lib/python3.8/asyncio/”, line 639, in get_event_loop raise RuntimeError(‘There is no current event loop in thread %r.’ RuntimeError: There is no current event loop in thread ‘ThreadPoolExecutor-0_0’.


I updated the code, the runtime error gone. but I am getting time out error. I put the timeout 180 secondes, but still when I test the function times out on 60 seconds.

Here is the updated code. Is there something I am doing wrong?

async def foo():
    from datetime import datetime, timedelta
    from pandas import DataFrame
    from pyrogram import Client
    import asyncio

    session_string = "********zNmkubA4ibjsdjhsdfjlhweruifnjkldfioY5DE*********"    
    api_id = 325511548224831351
    api_hash = "jdffjgtrkjhfklmrtgjtrm;sesews;;wex"        
    channel_name = 'cnn'

    with Client(session_string, api_id, api_hash, takeout=True) as app:
        hist_iter = app.iter_history(
            channel_name, limit=10)
        msglist = [msg.__dict__ for msg in hist_iter]
        df = DataFrame(msglist)    
    return df
async def bar():
    return await foo() 

def test(request):
    from asyncio import run
    return run(bar())


The solution in the end was to change from Pyrogram to telethon and create the asyncio manaually before creating the client.

loop = asyncio.new_event_loop()