pythonarchitecturesoftware-design

Software design for simultaneously running python programs


My question is not directly about the implementation in the code, but more about the basic design or technique I should use.

Initial situation: I have a Python program that executes a simulation. This should be started with different parameters at different times of the day. The different cases (simulations) are described in a config file. This file contains, for example, the name of the simulation, the times at which it should run and the specific parameters for this simulation.

What I have implemented so far: Currently the whole code is wrapped in a while true loop. In it, a relatively complex if-statement checks whether the current time matches one from the config file and starts all simulations that are to be started at this time.

My question: What is the best way to implement my initial situation in the best possible/correct way? What is the better/correct alternative to while true and a complex if statement? Should I separate my code into multiple files and write a parent control part? I have no idea how to implement something like this correctly. If you want to make decisions in your code, you have to learn control structures (if, else etc.). Which topic do I have to learn?


Solution

  • It sounds like you have implemented your own little cron job engine in your simulation program. For clarity and structure, it is probably better to keep the time scheduling part out of the simulation and instead to use the operation system's methods for this.

    So for example in Linux, you could set up a cron job for you needed time interval and just pass the technical parameters (name of simulation run and other values) to your program.

    An example:

    0 10 * * 3 /usr/bin/python /path/to/myscript.py 'Wednesday Simulation' arg1 arg2
    

    This will invoke myscript.py every Wednesday at 10:00 with the parameters 'Wednesday Simulation' (as the name, could be used in the output or in a generated simulation report) and arg1 and arg2.

    For Windows, the same thing can be done by using scheduled tasks.

    In your simulation program, you would then just remove the while loop and let it run just once with the parameters passed.