oscarwang2 commited on
Commit
167072a
1 Parent(s): c1a37ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -12
app.py CHANGED
@@ -6,7 +6,7 @@ import logging
6
  import psutil
7
  import subprocess
8
  from flask import Flask, request, jsonify, render_template, send_file
9
- import multiprocessing
10
 
11
  # Configure logging
12
  logging.basicConfig(level=logging.INFO)
@@ -15,9 +15,9 @@ logger = logging.getLogger(__name__)
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
- # Define the target function for multiprocessing
21
  def target_function(script_path, folder_path):
22
  output_log = tempfile.TemporaryFile(mode='w+t')
23
  try:
@@ -55,7 +55,7 @@ def handle_upload():
55
  with open(script_path, 'w') as script_file:
56
  script_file.write(script_content)
57
 
58
- # Run the script using multiprocessing
59
  log_output = run_script(script_path, folder_path)
60
 
61
  # Create a zip file of the entire folder
@@ -85,7 +85,8 @@ def get_cpu_info():
85
  try:
86
  info = []
87
  for host, data in connected_cpus.items():
88
- info.append(f"{host}: {data['cpu_count']} CPUs, {data['usage']}% usage")
 
89
  return jsonify({"status": "success", "cpu_info": "\n".join(info)})
90
  except Exception as e:
91
  logger.error(f"Error in get_cpu_info: {e}")
@@ -154,14 +155,21 @@ def index():
154
  return render_template('index.html')
155
 
156
  def run_script(script_path, folder_path):
157
- # Collect all available CPUs including the local host CPU
158
- total_cpus = sum(cpu['cpu_count'] for cpu in connected_cpus.values()) + 1
159
-
160
- # Use multiprocessing to run the script
161
- with multiprocessing.Pool(total_cpus) as pool:
162
- log_outputs = pool.starmap(target_function, [(script_path, folder_path)] * total_cpus)
163
 
164
- return '\n'.join(log_outputs)
 
 
 
 
 
 
 
 
 
 
165
 
166
  if __name__ == "__main__":
167
  app.run(host='0.0.0.0', port=7860, threaded=True)
 
6
  import psutil
7
  import subprocess
8
  from flask import Flask, request, jsonify, render_template, send_file
9
+ from mpi4py import MPI
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": psutil.cpu_percent(interval=1)}}
19
 
20
+ # Define the target function for MPI
21
  def target_function(script_path, folder_path):
22
  output_log = tempfile.TemporaryFile(mode='w+t')
23
  try:
 
55
  with open(script_path, 'w') as script_file:
56
  script_file.write(script_content)
57
 
58
+ # Run the script using MPI
59
  log_output = run_script(script_path, folder_path)
60
 
61
  # Create a zip file of the entire folder
 
85
  try:
86
  info = []
87
  for host, data in connected_cpus.items():
88
+ usage = psutil.cpu_percent(interval=1) if host == "localhost" else data['usage']
89
+ info.append(f"{host}: {data['cpu_count']} CPUs, {usage}% usage")
90
  return jsonify({"status": "success", "cpu_info": "\n".join(info)})
91
  except Exception as e:
92
  logger.error(f"Error in get_cpu_info: {e}")
 
155
  return render_template('index.html')
156
 
157
  def run_script(script_path, folder_path):
158
+ comm = MPI.COMM_WORLD
159
+ rank = comm.Get_rank()
160
+ size = comm.Get_size()
 
 
 
161
 
162
+ if rank == 0:
163
+ # Master process
164
+ log_outputs = []
165
+ for i in range(1, size):
166
+ log_output = comm.recv(source=i, tag=11)
167
+ log_outputs.append(log_output)
168
+ return '\n'.join(log_outputs)
169
+ else:
170
+ # Worker process
171
+ log_output = target_function(script_path, folder_path)
172
+ comm.send(log_output, dest=0, tag=11)
173
 
174
  if __name__ == "__main__":
175
  app.run(host='0.0.0.0', port=7860, threaded=True)