Paste Description for Python process management with C
http://www.defuze.org/archives/198-managing-your-process-with-the-cherrypy-bus.html
- Python process management with C
- Wednesday, June 9th, 2010 at 2:04:32pm MDT
- # -*- coding: utf-8 -*-
- import random
- import string
- import sys
- from multiprocessing import Process, Queue
- from Queue import Empty
- import logging
- from logging import handlers
- from cherrypy.process import wspbus
- class MyBus(wspbus.Bus):
- def __init__(self, name=""):
- wspbus.Bus.__init__(self)
- self.open_logger(name)
- self.subscribe("log", self._log)
- def exit(self):
- wspbus.Bus.exit(self)
- self.close_logger()
- def open_logger(self, name=""):
- logger = logging.getLogger(name)
- logger.setLevel(logging.INFO)
- h = logging.StreamHandler(sys.stdout)
- h.setLevel(logging.INFO)
- h.setFormatter(logging.Formatter("[%(asctime)s] %(name)s - %(levelname)s - %(message)s"))
- logger.addHandler(h)
- self.logger = logger
- def close_logger(self):
- for handler in self.logger.handlers:
- handler.flush()
- handler.close()
- def _log(self, msg="", level=logging.INFO):
- self.logger.log(level, msg)
- class Broker(Process):
- def __init__(self, queue):
- Process.__init__(self)
- self.queue = queue
- self.bus = MyBus(Broker.__name__)
- self.bus.subscribe("main", self.check)
- def check(self):
- try:
- message = self.queue.get_nowait()
- except Empty:
- return
- if message == "stop":
- self.bus.unsubscribe("main", self.check)
- self.bus.exit()
- elif message.startswith("BUY"):
- self.buy(*message.split(' ', 2)[1:])
- elif message.startswith("SELL"):
- self.sell(*message.split(' ', 2)[1:])
- def run(self):
- self.bus.start()
- self.bus.block(interval=0.01)
- def stop(self):
- self.queue.put("stop")
- def buy(self, code, amount):
- self.bus.log("BUY order placed for %s %s" % (amount, code))
- def sell(self, code, amount):
- self.bus.log("SELL order placed for %s %s" % (amount, code))
- class Bank(object):
- def __init__(self, queue):
- self.bus = MyBus(Bank.__name__)
- self.queue = queue
- self.bus.subscribe("main", self.randomly_place_order)
- self.bus.subscribe("exit", self.terminate)
- def randomly_place_order(self):
- order = random.sample(['BUY', 'SELL'], 1)[0]
- code = random.sample(string.ascii_uppercase, 4)
- amount = random.randint(0, 100)
- message = "%s %s %d" % (order, ''.join(code), amount)
- self.bus.log("Placing order: %s" % message)
- self.queue.put(message)
- def run(self):
- self.bus.start()
- self.bus.block(interval=0.01)
- def terminate(self):
- self.bus.unsubscribe("main", self.randomly_place_order)
- self.bus.unsubscribe("exit", self.terminate)
- if __name__ == '__main__':
- queue = Queue()
- broker = Broker(queue)
- broker.start()
- bank = Bank(queue)
- bank.run()
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.