Spaces:
Sleeping
Sleeping
oscarwang2
commited on
Commit
•
1dcef25
1
Parent(s):
12c761b
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ from zipfile import ZipFile
|
|
6 |
import logging
|
7 |
import json
|
8 |
import psutil
|
|
|
9 |
from flask import Flask, request, jsonify, render_template, send_file
|
10 |
|
11 |
# Configure logging
|
@@ -38,27 +39,32 @@ def update_cpu_usage_handler():
|
|
38 |
logger.info(f"Updated CPU usage for {host}: {usage}%")
|
39 |
return jsonify({"status": "success"})
|
40 |
|
41 |
-
# Function to run the provided Python script using
|
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 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
log_output
|
58 |
-
|
59 |
-
|
|
|
60 |
|
61 |
-
|
|
|
|
|
|
|
|
|
62 |
|
63 |
# Function to handle file uploads and script execution
|
64 |
@app.route('/upload', methods=['POST'])
|
|
|
6 |
import logging
|
7 |
import json
|
8 |
import psutil
|
9 |
+
import multiprocessing
|
10 |
from flask import Flask, request, jsonify, render_template, send_file
|
11 |
|
12 |
# Configure logging
|
|
|
39 |
logger.info(f"Updated CPU usage for {host}: {usage}%")
|
40 |
return jsonify({"status": "success"})
|
41 |
|
42 |
+
# Function to run the provided Python script using multiprocessing
|
43 |
def run_script(script_content, folder_path):
|
44 |
script_path = os.path.join(folder_path, 'user_script.py')
|
45 |
with open(script_path, 'w') as script_file:
|
46 |
script_file.write(script_content)
|
47 |
|
48 |
+
def target_function(cpu_id):
|
49 |
+
output_log = tempfile.TemporaryFile(mode='w+t')
|
50 |
+
try:
|
51 |
+
result = subprocess.run(['python', script_path], cwd=folder_path, stdout=output_log, stderr=subprocess.STDOUT)
|
52 |
+
output_log.seek(0)
|
53 |
+
log_output = output_log.read()
|
54 |
+
except Exception as e:
|
55 |
+
log_output = str(e)
|
56 |
+
finally:
|
57 |
+
output_log.close()
|
58 |
+
return log_output
|
59 |
+
|
60 |
+
# Collect all available CPUs including the local host CPU
|
61 |
+
total_cpus = sum(cpu['cpu_count'] for cpu in connected_cpus.values())
|
62 |
|
63 |
+
# Run the script using multiprocessing
|
64 |
+
with multiprocessing.Pool(total_cpus) as pool:
|
65 |
+
log_outputs = pool.map(target_function, range(total_cpus))
|
66 |
+
|
67 |
+
return '\n'.join(log_outputs)
|
68 |
|
69 |
# Function to handle file uploads and script execution
|
70 |
@app.route('/upload', methods=['POST'])
|