我可以在GCP云功能内使用python-aiohttp吗?

时间:2020-10-30 00:20:29

标签: asynchronous flask google-cloud-platform io aiohttp

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

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

1 个答案:

答案 0 :(得分:0)

您不能直接使用asyncio,python运行时已沙盒化,所以insert into target table (c_id, c_lname, c_mname, c_fname, c_document, c_email, c_dob) select (c_id, c_lname, c_mname, c_fname, c_document, c_email, c_dob) from source_table s where not exists (select 1 from target_table t where t.c_document=s.c_document); 将出错。

但是有一种方法可以实现异步请求分发,但是(此处模糊地)记载了以下内容:

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

这也适用于Cloud Functions。