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
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:
This works for Cloud Functions as well.