コード例 #1
0
ファイル: worker.py プロジェクト: pombredanne/flower-1
    def __init__(self, conf, log, age=0, ppid=0):

        self.name = conf.sname

        self.log = log
        self.age = age
        self.ppid = ppid
        self.timeout = conf.timeout
        self.conf = conf


        # initialize
        self.booted = False
        self.alive = True
        self.tmp = WorkerTmp(self.conf)
コード例 #2
0
ファイル: worker.py プロジェクト: pombredanne/flower-1
class Worker(object):

    _SIGNALS = map(
        lambda x: getattr(signal, "SIG%s" % x),
        "HUP QUIT INT TERM USR1 USR2 WINCH CHLD".split()
    )
    
    _PIPE = []

    def __init__(self, conf, log, age=0, ppid=0):

        self.name = conf.sname

        self.log = log
        self.age = age
        self.ppid = ppid
        self.timeout = conf.timeout
        self.conf = conf


        # initialize
        self.booted = False
        self.alive = True
        self.tmp = WorkerTmp(self.conf)


    def pid(self):
        return os.getpid()
    pid = util.cached_property(pid)

    def notify(self):
        """\
        Your worker subclass must arrange to have this method called
        once every ``self.timeout`` seconds. If you fail in accomplishing
        this task, the master process will murder your workers.
        """
        self.tmp.notify()


    def run(self):
        """\
        This is the mainloop of a worker process. You should override
        this method in a subclass to provide the intended behaviour
        for your particular evil schemes.
        """
        while True:
            self.notify()
            gevent.sleep(0.1)

    def init_process(self):
        """\
        If you override this method in a subclass, the last statement
        in the function should be to call this method with
        super(MyWorkerClass, self).init_process() so that the ``run()``
        loop is initiated.
        """
        util.set_owner_process(self.conf.uid, self.conf.gid)

        # Reseed the random number generator
        util.seed()

        # For waking ourselves up
        self._PIPE = os.pipe()
        map(util.set_non_blocking, self._PIPE)
        map(util.close_on_exec, self._PIPE)
        
        # Prevent fd inherientence
        util.close_on_exec(self.tmp.fileno())
        self.init_signals()
        
        # Enter main run loop
        self.booted = True
        self.run()

    def init_signals(self):
        map(lambda s: signal.signal(s, signal.SIG_DFL), self._SIGNALS)
        signal.signal(signal.SIGQUIT, self.handle_quit)
        signal.signal(signal.SIGTERM, self.handle_exit)
        signal.signal(signal.SIGINT, self.handle_exit)
        signal.signal(signal.SIGWINCH, self.handle_winch)
            
    def handle_quit(self, sig, frame):
        self.alive = False

    def handle_exit(self, sig, frame):
        self.alive = False
        sys.exit(0)
        
    def handle_winch(self, sig, fname):
        # Ignore SIGWINCH in worker. Fixes a crash on OpenBSD.
        return