コード例 #1
0
ファイル: process.py プロジェクト: pyloque/gkron
 def __init__(self, child_num):
     self.child_num = child_num
     self.child_map = {} # pid=>fd
     self.child_fd_in_busy = set()
     self.handlers = HandlerMap()
     self.buffer = Buffer()
     self.pending_task_ids = []
     self.make_daemon()
     self.store = RedisTaskStore('localhost', 6379, '9')
     self.delayed_tasks = TaskManager(self.store)
     self.stoped = False
コード例 #2
0
ファイル: process.py プロジェクト: pyloque/gkron
class Process(object):

    def __init__(self, child_num):
        self.child_num = child_num
        self.child_map = {} # pid=>fd
        self.child_fd_in_busy = set()
        self.handlers = HandlerMap()
        self.buffer = Buffer()
        self.pending_task_ids = []
        self.make_daemon()
        self.store = RedisTaskStore('localhost', 6379, '9')
        self.delayed_tasks = TaskManager(self.store)
        self.stoped = False

    def make_daemon(self):
        daemon_init("/var/log/gkron/gkron.out", "/var/log/gkron/gkron.err")

    def start(self):
        for i in range(self.child_num):
            self.fork_child()
        self.register_handlers()
        self.register_signals()
        while not self.stoped:
            self.interact()
        self.wait_children_to_die()

    def add_task(self, task):
        self.delayed_tasks.add_task(task)

    def clear_all_tasks(self):
        self.delayed_tasks.clear_all()

    def fork_child(self):
        parent, child = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
        parent.setblocking(0)
        child.setblocking(0)
        pid = os.fork()
        if pid:
            self.father(pid, parent, child)
        else:
            self.child(parent, child)

    def child(self, parent, child):
        child.close()
        setproctitle('gkron worker')
        Worker(parent, self.store).start()
        sys.exit(0)

    def father(self, pid, parent, child):
        parent.close()
        self.child_map[pid] = child
        setproctitle('gkron master')

    def wait_children_to_die(self):
        child_fds = self.child_map.values()
        for child in child_fds:
            self.send_child(child, 'bye:')
        for pid in self.child_map:
            try:
                os.waitpid(pid, os.P_WAIT)
            except OSError, ex:
                if ex[0] == errno.EINTR:
                    pass
                else:
                    raise
        print 'father is over'