asyncio not working on Google Cloud Functions

半腔热情 提交于 2021-02-20 02:57:43

问题


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.datetime.now() - 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)
        print(df.head(5))
 
    return f'it works!:{request_json}'

The error message I get from GCF log:

File "/opt/python3.8/lib/python3.8/asyncio/events.py", 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'.

Update

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())

回答1:


  1. bar() is redundant
  2. You're trying to return a dataframe. Is it a valid HTTP response?
  3. with -> async with
  4. hist_iter = app.iter_history() -> hist_iter = await app.iter_history()
  5. M.b. it waits for input?



回答2:


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()
asyncio.set_event_loop(loop)


来源:https://stackoverflow.com/questions/66095735/asyncio-not-working-on-google-cloud-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!