From 54347ded894e6de3372416cc0d6ab17f6dc4a812 Mon Sep 17 00:00:00 2001 From: Peter Stockings Date: Wed, 23 Jul 2025 22:11:52 +1000 Subject: [PATCH] Make requests to isolator service asynchronous --- app.py | 28 ++++++++++++++-------------- requirements.txt | 2 +- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/app.py b/app.py index 800f1ea..d831bdf 100644 --- a/app.py +++ b/app.py @@ -78,7 +78,7 @@ def documentation(): return render_template("documentation.html") @app.route('/execute', methods=['POST']) -def execute_code(): +async def execute_code(): try: # Extract code and convert request to a format acceptable by Node.js app code = request.json.get('code') @@ -92,9 +92,10 @@ def execute_code(): environment = request.json.get('environment_info') environment_json = json.loads(environment) - # Call the Node.js API - response = requests.post(API_URL, json={'code': code, 'request': request_obj, 'environment': environment_json, 'name': "anonymous"}) - response_data = response.json() + # Call the Node.js API asynchronously + async with aiohttp.ClientSession() as session: + async with session.post(API_URL, json={'code': code, 'request': request_obj, 'environment': environment_json, 'name': "anonymous"}) as response: + response_data = await response.json() # check if playground=true is in the query string if request.args.get('playground') == 'true': @@ -104,12 +105,11 @@ def execute_code(): flask_response = map_isolator_response_to_flask_response(response_data) return flask_response - except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/f//', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD']) -def execute_http_function(user_id, function): +async def execute_http_function(user_id, function): try: # Split the function_path into the function name and the sub-path parts = function.split('/', 1) @@ -175,16 +175,17 @@ def execute_http_function(user_id, function): if request.data and not request.is_json: request_data['text'] = request.data.decode('utf-8') - # Call the Node.js API - response = requests.post(API_URL, json={'code': code, 'request': request_data, 'environment': environment, 'name': function_name}) - response_data = response.json() + # Call the Node.js API asynchronously + async with aiohttp.ClientSession() as session: + async with session.post(API_URL, json={'code': code, 'request': request_data, 'environment': environment, 'name': function_name}) as response: + response_data = await response.json() db.update_http_function_environment_info_and_invoked_count(user_id, function_name, response_data['environment']) db.add_http_function_invocation( - http_function['id'], - response_data['status'], - request_data if log_request else {}, - response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {}, + http_function['id'], + response_data['status'], + request_data if log_request else {}, + response_data['result'] if (log_response or response_data['status'] != 'SUCCESS') else {}, response_data['logs'], version_number) @@ -195,7 +196,6 @@ def execute_http_function(user_id, function): flask_response = map_isolator_response_to_flask_response(response_data) return flask_response - except Exception as e: return jsonify({'error': str(e)}), 500 diff --git a/requirements.txt b/requirements.txt index b0858c8..b393197 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -Flask==2.2.5 +Flask[async]==2.2.5 gunicorn==19.7.1 Jinja2==3.1.0 jinja-partials==0.1.1