From: Michael Tremer Date: Fri, 21 Dec 2012 15:38:31 +0000 (+0100) Subject: scheduler: Add function to fork worker process. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=52d18d04ad13a8c49a2f5f4ba4472c1f2bb52325;p=pbs.git scheduler: Add function to fork worker process. It is much better to let some jobs run in their own process, because that helps us to deal with memory-leaks and saves the main process from being terminated by an unforeseen error. --- diff --git a/backend/scheduler.py b/backend/scheduler.py index 80f5fc4b..9e08362e 100644 --- a/backend/scheduler.py +++ b/backend/scheduler.py @@ -1,16 +1,20 @@ #!/usr/bin/python import logging +import multiprocessing import time import traceback +import backend.main + class Event(object): interval = None priority = 0 - def __init__(self, *arguments): - self.arguments = arguments + def __init__(self, *args, **kwargs): + self.args = args + self.kwargs = kwargs self._next_start_time = 0 @@ -26,6 +30,30 @@ class Event(object): def run(self, *args, **kwargs): raise NotImplemented + def run_subprocess_background(self, method, *args): + arguments = [method,] + list(args) + + process = multiprocessing.Process(target=self.fork, args=arguments) + process.daemon = False + + # Start the process. + process.start() + + return process + + def run_subprocess(self, *args): + process = self.run_subprocess_background(*args) + + # Wait until process has finished. + process.join() + + @staticmethod + def fork(method, *args, **kwargs): + # Create new pakfire instance. + _pakfire = backend.main.Pakfire() + + return method(_pakfire, *args, **kwargs) + class Scheduler(object): def __init__(self): @@ -48,6 +76,7 @@ class Scheduler(object): def run(self): while self._queue: self.sort_queue() + print self._queue for event in self._queue: # If the event has to be started some time in @@ -56,7 +85,7 @@ class Scheduler(object): try: logging.info("Running %s..." % event) - event.run(*event.arguments) + event.run(*event.args, **event.kwargs) # In case the user interrupts the scheduler. except KeyboardInterrupt: