Skip to content

Commit

Permalink
big changes. allow an arbiter to handler multiple kind of workers. Also
Browse files Browse the repository at this point in the history
allows it to chain multiple arbiters (an arbiter can be lau ched like
any worker)
  • Loading branch information
benoitc committed Aug 7, 2011
1 parent 914dea3 commit c87a658
Show file tree
Hide file tree
Showing 12 changed files with 1,273 additions and 319 deletions.
73 changes: 72 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,75 @@ pistil
------


Simple multiprocessing toolkit. This is based on the `Gunicorn <http://gunicorn.orh>`_ multiprocessing engine.
Simple multiprocessing toolkit. This is based on the `Gunicorn <http://gunicorn.orh>`_ multiprocessing engine.

This library allows you to supervise multiple type of workers and chain
supervisors. Gracefull, reload, signaling between workers is handled.

Ex::

import time
import urllib2

from pistil.arbiter import Arbiter, child
from pistil.worker import Worker
from pistil.tcp.sync_worker import TcpSyncWorker
from pistil.tcp.arbiter import TcpArbiter

from http_parser.http import HttpStream
from http_parser.reader import SocketReader

class MyTcpWorker(TcpSyncWorker):

def handle(self, sock, addr):
p = HttpStream(SocketReader(sock))

path = p.path()
data = "welcome world"
sock.send("".join(["HTTP/1.1 200 OK\r\n",
"Content-Type: text/html\r\n",
"Content-Length:" + str(len(data)) + "\r\n",
"Connection: close\r\n\r\n",
data]))


class UrlWorker(Worker):

def run(self):
print "ici"
while self.alive:
time.sleep(0.1)
f = urllib2.urlopen("http://localhost:5000")
print f.read()
self.notify

class MyPoolArbiter(TcpArbiter):
worker = child(MyTcpWorker, 30, "worker", {})


class MyArbiter(Arbiter):
pool = child(MyPoolArbiter, 30, "supervisor",
{"num_workers": 3, "address": ("127.0.0.1", 5000)})
grabber = child(UrlWorker, 30, "worker", {})

if __name__ == '__main__':
arbiter = MyArbiter("master", local_conf={"num_workers": 3})
arbiter.run()


This examplelaunch a web server with 3 workers on port 5000 and another
worker fetching the welcome page hosted by this server::


$ python examples/multiworker2.py

2011-08-08 00:05:42 [13195] [DEBUG] Arbiter master booted on 13195
2011-08-08 00:05:42 [13196] [INFO] Booting grabber (worker) with pid: 13196
ici
2011-08-08 00:05:42 [13197] [INFO] Booting pool (supervisor) with pid: 13197
2011-08-08 00:05:42 [13197] [DEBUG] Arbiter pool booted on 13197
2011-08-08 00:05:42 [13197] [INFO] Listening at: http://127.0.0.1:5000 (13197)
2011-08-08 00:05:42 [13198] [INFO] Booting worker (worker) with pid: 13198
2011-08-08 00:05:42 [13199] [INFO] Booting worker (worker) with pid: 13199
welcome world
welcome world
Binary file added examples/.hello.py.swo
Binary file not shown.
39 changes: 39 additions & 0 deletions examples/multiworker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -
#
# This file is part of pistil released under the MIT license.
# See the NOTICE for more information.

import time
from pistil.arbiter import Arbiter, child
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()

class MyWorker2(Worker):

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


class MyArbiter(Arbiter):
worker = child(MyWorker, 30, "worker", {})
worker2 = child(MyWorker2, 30, "worker", {})
worker3 = child(MyWorker2, 30, "kill", {})


if __name__ == '__main__':
arbiter = MyArbiter("master")

arbiter.run()
56 changes: 56 additions & 0 deletions examples/multiworker2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -
#
# This file is part of pistil released under the MIT license.
# See the NOTICE for more information.

import time
import urllib2

from pistil.arbiter import Arbiter, child
from pistil.worker import Worker
from pistil.tcp.sync_worker import TcpSyncWorker
from pistil.tcp.arbiter import TcpArbiter

from http_parser.http import HttpStream
from http_parser.reader import SocketReader

class MyTcpWorker(TcpSyncWorker):

def handle(self, sock, addr):
p = HttpStream(SocketReader(sock))

path = p.path()
data = "welcome wold"
sock.send("".join(["HTTP/1.1 200 OK\r\n",
"Content-Type: text/html\r\n",
"Content-Length:" + str(len(data)) + "\r\n",
"Connection: close\r\n\r\n",
data]))


class UrlWorker(Worker):

def run(self):
print "ici"
while self.alive:
time.sleep(0.1)
f = urllib2.urlopen("http://localhost:5000")
print f.read()
self.notify

class MyPoolArbiter(TcpArbiter):
worker = child(MyTcpWorker, 30, "worker", {})


class MyArbiter(Arbiter):
pool = child(MyPoolArbiter, 30, "supervisor",
{"num_workers": 3, "address": ("127.0.0.1", 5000)})
grabber = child(UrlWorker, 30, "worker", {})

if __name__ == '__main__':
arbiter = MyArbiter("master", local_conf={"num_workers": 3})
arbiter.run()




34 changes: 34 additions & 0 deletions examples/tcp_hello.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# -*- coding: utf-8 -
#
# This file is part of pistil released under the MIT license.
# See the NOTICE for more information.

from pistil.tcp.sync_worker import TcpSyncWorker
from pistil.tcp.arbiter import TcpArbiter

from http_parser.http import HttpStream
from http_parser.reader import SocketReader

class MyTcpWorker(TcpSyncWorker):

def handle(self, sock, addr):
p = HttpStream(SocketReader(sock))

path = p.path()
data = "welcome wold"
sock.send("".join(["HTTP/1.1 200 OK\r\n",
"Content-Type: text/html\r\n",
"Content-Length:" + str(len(data)) + "\r\n",
"Connection: close\r\n\r\n",
data]))


class MyPoolArbiter(TcpArbiter):
worker = child(MyTcpWorker, 30, "worker", {})


if __name__ == '__main__':
arbiter = MyPoolArbiter("master", local_conf={"num_workers": 3})

arbiter.run()

Loading

0 comments on commit c87a658

Please sign in to comment.