Skip to content

Commit

Permalink
fix the arbiter. Make sure it notify the master when he's up.
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitc committed Aug 8, 2011
1 parent 1efa782 commit 6c53324
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
18 changes: 18 additions & 0 deletions examples/hello_pool.py
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -
#
# This file is part of pistil released under the MIT license.
# See the NOTICE for more information.

from pistil.pool import PoolArbiter
from pistil.worker import Worker

class MyWorker(Worker):

def handle(self):
print "hello from worker n°%s" % self.pid

if __name__ == "__main__":
conf = {"num_workers": 3 }
spec = (MyWorker, 30, "worker", {}, "test",)
a = PoolArbiter(conf, spec)
a.run()
24 changes: 6 additions & 18 deletions examples/multiworker.py
Expand Up @@ -3,41 +3,29 @@
# This file is part of pistil released under the MIT license.
# See the NOTICE for more information.

import time
from pistil.arbiter import Arbiter
from pistil.worker import Worker
from http_parser.http import HttpStream
from http_parser.reader import SocketReader


class MyWorker(Worker):

def run(self):
while self.alive:
time.sleep(0.1)
print "hello %s" % self.name
self.notify()
def handle(self):
print "hello worker 1 from %s" % self.name

class MyWorker2(Worker):

def run(self):
print "yo"
while self.alive:
time.sleep(0.1)
print "hello 2 %s" % self.name
self.notify()
def handle(self):
print "hello worker 2 from %s" % self.name


if __name__ == '__main__':

conf = {}

specs = [
(MyWorker, 30, "worker", {}, "w1"),
(MyWorker2, 30, "worker", {}, "w2"),
(MyWorker2, 30, "kill", {}, "w3")
]


# launchh the arbiter
arbiter = Arbiter(conf, specs)

arbiter.run()
7 changes: 6 additions & 1 deletion pistil/arbiter.py
Expand Up @@ -150,6 +150,9 @@ def init_process(self):
# Reseed the random number generator
util.seed()

# prevent fd inheritance
util.close_on_exec(self.tmp.fileno())

# init signals
self.init_signals()

Expand Down Expand Up @@ -195,6 +198,8 @@ def run(self):
self.spawn_workers()
while True:
try:
# notfy the master
self.tmp.notify()
self.reap_workers()
sig = self._SIG_QUEUE.pop(0) if len(self._SIG_QUEUE) else None
if sig is None:
Expand Down Expand Up @@ -257,7 +262,6 @@ def handle_term(self):
self.stop(False)
raise StopIteration


def handle_usr1(self):
"""\
SIGUSR1 handling.
Expand All @@ -283,6 +287,7 @@ def wakeup(self):
except IOError, e:
if e.errno not in [errno.EAGAIN, errno.EINTR]:
raise


def halt(self, reason=None, exit_status=0):
""" halt arbiter """
Expand Down
1 change: 0 additions & 1 deletion pistil/pool.py
Expand Up @@ -107,7 +107,6 @@ def reload(self):
# manage workers
self.manage_workers()


def reap_workers(self):
"""\
Reap workers to avoid zombie processes
Expand Down

0 comments on commit 6c53324

Please sign in to comment.