Toto Services¶
TotoService can be used to write general processes that take advantage of the process creation/management features used by TotoServer and TotoWorker - the two built in subclasses of TotoService. TotoService subclasses can be run with the --start (or --stop) and --processes options to start the service as a daemon process or run multiple instances simultaneously.
To run a subclass of TotoService create a script like this:
from toto.service import TotoService
class MyServiceSubclass(TotoService):
def main_loop(self):
while 1:
#run some job continuously
MyServiceSubclass('conf_file.conf').run()
- class toto.service.TotoService(conf_file=None, **kwargs)[source]¶
Subclass TotoService to create a service that can be easily daemonised or ran in multiple processes simultaneously.
- TotoService.run()[source]¶
Start the service. Depending on the initialization options, this may run more than one service process.
- TotoService.main_loop()[source]¶
Subclass TotoService and override main_loop() with your desired functionality.
- TotoService.prepare()[source]¶
Override this method in a TotoService subclass and it will be called before any service processes are created. You can set instance variables here and they will be available in main_loop() but be careful that any retained objects are safe to access across processes
- TotoService.finish()[source]¶
Override this method in a TotoService subclass and it will be called after all service processes have exited (after each main_loop() has returned).
Note: This method will only be called once and only after all child processes have finished.
Utility Functions¶
- toto.service.process_count()[source]¶
Returns the number of service processes that will run with the current configuration. This will match the --processes=n option if n >= 0. Otherwise multiprocessing.cpu_count() will be used.
- toto.service.pid_path(i)[source]¶
Used to generate PID files for daemonized TotoServices. Child processes with PID files matching the paths returned by this function will be killed with SIGTERM when the server daemon process is stopped using the --stop or --daemon=stop arguments:
proc = Process() proc.start() with open(pid_path(process_count() + 1), 'wb') as f: f.write(str(proc.pid))
Note that i must be an integer.