我可以在GCP云功能内使用python-aiohttp吗? [英] Can I use python - aiohttp inside of a GCP cloud function?

查看:84
本文介绍了我可以在GCP云功能内使用python-aiohttp吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些工作负载路由/分配给其他云功能,它们全部花费大致相同的时间,但是我必须同步调用它们.我尝试进行设置,并在使用Flask实现它时在瓶上下文之外实例化我的循环时遵循了我的建议,因为它需要控制主线程(也要返回),并且一切似乎仍在同步执行,我只需要在对象中收集一些响应.这个简单的I/O问题似乎不适用于GCP云功能.我已经看过这个链接是否被询问的地方甚至有可能.多数民众赞成在纯asynchio,所以这应该工作.如果有人在工作之前就可以做到这一点,您可以分享一个例子吗?

I'm trying to route/divvy up some workload to other cloud functions they all take roughly about the same amount of time, but I have to invoke them synchronously. I tried setting this up, and I followed someone's advice when implementing it with Flask to instantiate my loop outside the flask context because it needs control of the main thread(to return too), and everything still seems to be executed synchronously, I just need to gather up some responses in an object. this simple I/O problem doesn't seem to work on GCP Cloud Functions. I've seen this link where it's asked if it's even possible. Thats just plain asynchio, so this should work. if someone has gotten this to work before could you share an example?

loop = asyncio.get_event_loop()
app = Flask(__name__)
CORS(app)

@app.route('/', methods=['POST'])
def calculations():
    calculations_to_perform_queue = []
            for calculation_request in request_data['calculations']:
                for calculation in calculation_request['calculation_requests']:
                    calculations_to_perform_queue.append(calculation)

    async def threaded_calculation_request_operation(session, 
        calculation_meta_data) -> dict: reference in calculations if exists
        url = calculation_meta_data['endpoint_url']
        async with session.post(
            url=url,
            headers={'X-Access-Token': request.headers['X-Access-Token']},
            json=json_safe_response(calculation_meta_data) as response:
        return await response.json()


    async def session_controler():
        async with aiohttp.ClientSession() as session:
            finished_queue = []
            for calculation in calculations_to_perform_queue:
                response = await threaded_calculation_request_operation(session, calculation)
                finished_queue.append(response)
            return finished_queue

    all_returned_charts = loop.run_until_complete(session_controler())
    prccesed_response = processed_for_return(all_returned_charts)
    return prccesed_response, 200

推荐答案

您不能直接使用asyncio,python运行时已沙盒化,所以 asyncio.get_event_loop()将出错.

You can't use asyncio directly, the python runtime is sandboxed so asyncio.get_event_loop() will error.

但是有一种方法可以实现异步请求分配,但是(在这里)(模糊地)记录了这一点:

There is a way to achieve asynchronous request dispatch however as (obscurely) documented here:

https://cloud.google.com/appengine/docs/standard/python/migrate-to-python3/migrate-outbound-requests#making_asynchronous_https_requests

这也适用于Cloud Functions.

This works for Cloud Functions as well.

这篇关于我可以在GCP云功能内使用python-aiohttp吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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