2020-10-30

Can I use python - aiohttp inside of a GCP cloud function? [closed]

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


from Recent Questions - Stack Overflow https://ift.tt/3e7wrrU
https://ift.tt/eA8V8J

No comments:

Post a Comment