import sys import redis import json import time import subprocess import psutil if __name__=='__main__': if len(sys.argv)<=1: configFN = 'config-remote.json' else: configFN = sys.argv[1] f = open(configFN) config = json.load(f) redis_address = config['redis']['address'] redis_password = config['redis']['password'] redis_port = config['redis']['port'] log_level = config['logging'] work_dir = config['work_dir'] process_name = config['process_name'] r = redis.Redis(host=redis_address, port=redis_port, decode_responses=True,password=redis_password) runnings = [] while True: sims = r.get('Simulations') sims = json.loads(sims) for sim in sims: simulation_id = sim['Id'] process_exists = False for proc in psutil.process_iter(['pid', 'name', 'cmdline']): if proc.info['name'] == process_name and len(proc.info['cmdline']) > 1 and proc.info['cmdline'][1] == simulation_id: process_exists = True break if not process_exists: subprocess.Popen(['./SimulationCore2', simulation_id], cwd=work_dir) time.sleep(1) # Connect to Redis server redis_host = 'localhost' # Replace with your Redis server's host redis_port = 6379 # Replace with your Redis server's port redis_client = redis.Redis(host=redis_host, port=redis_port) if redis_client is None: print(f"Can not connect to {redis_host}.\nExiting...") exit(1) # Main loop while True: # Retrieve array from Redis simulations = redis_client.lrange('simulations', 0, -1) # Check each entry in the array for entry in simulations: simulation_id = entry.decode() # Check if 'SimulationCore2' process with specific parameter is running process_name = 'SimulationCore2' process_exists = False for proc in psutil.process_iter(['pid', 'name', 'cmdline']): if proc.info['name'] == process_name and len(proc.info['cmdline']) > 1 and proc.info['cmdline'][1] == simulation_id: process_exists = True break if not process_exists: # Run 'SimulationCore2' process with specific parameter printf(f"Starting new simulator process for {simulation_id}") subprocess.Popen(['./SimulationCore2', simulation_id], cwd='/path/to/SimulationCore2/directory') # Wait for 1 second before checking again time.sleep(1)