Simulation Core
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

75 line
2.6 KiB

  1. import sys
  2. import redis
  3. import json
  4. import time
  5. import subprocess
  6. import psutil
  7. if __name__=='__main__':
  8. if len(sys.argv)<=1:
  9. configFN = 'config-remote.json'
  10. else:
  11. configFN = sys.argv[1]
  12. f = open(configFN)
  13. config = json.load(f)
  14. redis_address = config['redis']['address']
  15. redis_password = config['redis']['password']
  16. redis_port = config['redis']['port']
  17. log_level = config['logging']
  18. work_dir = config['work_dir']
  19. process_name = config['process_name']
  20. r = redis.Redis(host=redis_address, port=redis_port, decode_responses=True,password=redis_password)
  21. runnings = []
  22. while True:
  23. sims = r.get('Simulations')
  24. sims = json.loads(sims)
  25. for sim in sims:
  26. simulation_id = sim['Id']
  27. process_exists = False
  28. for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
  29. if proc.info['name'] == process_name and len(proc.info['cmdline']) > 1 and proc.info['cmdline'][1] == simulation_id:
  30. process_exists = True
  31. break
  32. if not process_exists:
  33. subprocess.Popen(['./SimulationCore2', redis_address,str(redis_port),redis_password,simulation_id,str(log_level)], cwd=work_dir)
  34. time.sleep(5)
  35. # Connect to Redis server
  36. redis_host = 'localhost' # Replace with your Redis server's host
  37. redis_port = 6379 # Replace with your Redis server's port
  38. redis_client = redis.Redis(host=redis_host, port=redis_port)
  39. if redis_client is None:
  40. print(f"Can not connect to {redis_host}.\nExiting...")
  41. exit(1)
  42. # Main loop
  43. while True:
  44. # Retrieve array from Redis
  45. simulations = redis_client.lrange('simulations', 0, -1)
  46. # Check each entry in the array
  47. for entry in simulations:
  48. simulation_id = entry.decode()
  49. # Check if 'SimulationCore2' process with specific parameter is running
  50. process_name = 'SimulationCore2'
  51. process_exists = False
  52. for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
  53. if proc.info['name'] == process_name and len(proc.info['cmdline']) > 1 and proc.info['cmdline'][1] == simulation_id:
  54. process_exists = True
  55. break
  56. if not process_exists:
  57. # Run 'SimulationCore2' process with specific parameter
  58. printf(f"Starting new simulator process for {simulation_id}")
  59. subprocess.Popen(['./SimulationCore2', simulation_id], cwd='/path/to/SimulationCore2/directory')
  60. # Wait for 1 second before checking again
  61. time.sleep(1)