Oscar Wang commited on
Commit
9e95bc6
·
verified ·
1 Parent(s): f84507f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -7,7 +7,6 @@ import logging
7
  import json
8
  import psutil
9
  from flask import Flask, request, jsonify, render_template, send_file
10
- import threading
11
 
12
  # Configure logging
13
  logging.basicConfig(level=logging.INFO)
@@ -16,7 +15,7 @@ logger = logging.getLogger(__name__)
16
  # Initialize Flask app
17
  app = Flask(__name__)
18
 
19
- connected_cpus = {}
20
 
21
  # Endpoint to donate CPU resources
22
  @app.route('/donate_cpu', methods=['POST'])
@@ -40,14 +39,18 @@ def update_cpu_usage_handler():
40
  return jsonify({"status": "success"})
41
 
42
  # Function to run the provided Python script using MPI
43
- def run_script(script_name, folder_path):
 
 
 
 
44
  output_log = tempfile.TemporaryFile(mode='w+t')
45
  try:
46
- # Collect all available CPUs
47
  total_cpus = sum(cpu['cpu_count'] for cpu in connected_cpus.values())
48
 
49
  # Run the script using MPI
50
- result = subprocess.run(['mpiexec', '-n', str(total_cpus), 'python', script_name], cwd=folder_path, stdout=output_log, stderr=subprocess.STDOUT)
51
  output_log.seek(0)
52
  log_output = output_log.read()
53
  except Exception as e:
@@ -60,11 +63,11 @@ def run_script(script_name, folder_path):
60
  # Function to handle file uploads and script execution
61
  @app.route('/upload', methods=['POST'])
62
  def handle_upload():
63
- if 'file' not in request.files or 'script_name' not in request.form:
64
- return jsonify({"status": "error", "message": "File or script name not provided"}), 400
65
 
66
  files = request.files.getlist('file')
67
- script_name = request.form['script_name']
68
 
69
  # Create a temporary directory to store uploaded files
70
  temp_dir = tempfile.mkdtemp()
@@ -77,7 +80,7 @@ def handle_upload():
77
  file_obj.save(file_path)
78
 
79
  # Run the script
80
- log_output = run_script(script_name, folder_path)
81
 
82
  # Create a zip file of the entire folder (including any new files created by the script)
83
  zip_path = os.path.join(temp_dir, 'output_folder.zip')
 
7
  import json
8
  import psutil
9
  from flask import Flask, request, jsonify, render_template, send_file
 
10
 
11
  # Configure logging
12
  logging.basicConfig(level=logging.INFO)
 
15
  # Initialize Flask app
16
  app = Flask(__name__)
17
 
18
+ connected_cpus = {"localhost": {"cpu_count": psutil.cpu_count(logical=False), "usage": 0.0}}
19
 
20
  # Endpoint to donate CPU resources
21
  @app.route('/donate_cpu', methods=['POST'])
 
39
  return jsonify({"status": "success"})
40
 
41
  # Function to run the provided Python script using MPI
42
+ def run_script(script_content, folder_path):
43
+ script_path = os.path.join(folder_path, 'user_script.py')
44
+ with open(script_path, 'w') as script_file:
45
+ script_file.write(script_content)
46
+
47
  output_log = tempfile.TemporaryFile(mode='w+t')
48
  try:
49
+ # Collect all available CPUs including the local host CPU
50
  total_cpus = sum(cpu['cpu_count'] for cpu in connected_cpus.values())
51
 
52
  # Run the script using MPI
53
+ result = subprocess.run(['mpiexec', '-n', str(total_cpus), 'python', script_path], cwd=folder_path, stdout=output_log, stderr=subprocess.STDOUT)
54
  output_log.seek(0)
55
  log_output = output_log.read()
56
  except Exception as e:
 
63
  # Function to handle file uploads and script execution
64
  @app.route('/upload', methods=['POST'])
65
  def handle_upload():
66
+ if 'file' not in request.files or 'script_content' not in request.form:
67
+ return jsonify({"status": "error", "message": "File or script content not provided"}), 400
68
 
69
  files = request.files.getlist('file')
70
+ script_content = request.form['script_content']
71
 
72
  # Create a temporary directory to store uploaded files
73
  temp_dir = tempfile.mkdtemp()
 
80
  file_obj.save(file_path)
81
 
82
  # Run the script
83
+ log_output = run_script(script_content, folder_path)
84
 
85
  # Create a zip file of the entire folder (including any new files created by the script)
86
  zip_path = os.path.join(temp_dir, 'output_folder.zip')