asyncio无法在Google Cloud Functions上运行 [英] asyncio not working on Google Cloud Functions

查看:76
本文介绍了asyncio无法在Google Cloud Functions上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有此功能,该功能在使用python 3.8的计算机上可以在本地正常运行,但会在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.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}'

我从GCF日志中收到的错误消息:

文件"/opt/python3.8/lib/python3.8/asyncio/events.py"的第639行get_event_loop引发RuntimeError('线程%r.RuntimeError:线程中没有当前事件循环"ThreadPoolExecutor-0_0".

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'.

更新

我更新了代码,运行时错误消失了.但我收到超时错误.我将超时设置为180秒,但是当我测试该功能时仍然是60秒超时.

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.

这是更新的代码.我在做错什么吗?

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

推荐答案

最终的解决方案是在创建客户端之前,从热像仪更改为Telethon并手动创建异步.

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)

这篇关于asyncio无法在Google Cloud Functions上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆