python - Daemon/Multiprocessing communicate through web.py -
python - Daemon/Multiprocessing communicate through web.py -
this im trying do:
create queue x pool workers. manage queue web.py add items view status (workres running, sleeping) maybe going @ wrong way? should create daemon handels queue , /script/ web.py communicates daemon on tcp or else?so, have made queue , worker class loads before web.py code loads (not in if main, if seek start there web.app dont know class.)
what seems down-side is seems have habbit of creating new processes when reload page. should load them somewhere else?
this log starting app (should not start 4 processes)
starting engine-9589
starting engine-9590
http://127.0.0.1:6077/
starting engine-9711
starting engine-9712
import web import procname import json, time, random import re import multiprocessing mp datetime import datetime import os procname.setprocname('my-engine') urls = ( '/', 'index', ) class queue: def __init__(self): self.queue = mp.queue() self.items = [] def put(self, item): self.queue.put(item) def isempty(self): homecoming self.queue.empty() def size(self): homecoming self.queue.qsize() class workers: pool_size = 2 init = true def __init__(self): if self.init == true: self.pool = mp.pool(processes=self.pool_size, initializer=self.start_worker,) self.init = false def start_worker(self): pid = os.getpid() prcname = "engine-%d" % pid mp.current_process().name = prcname procname.setprocname(prcname) print "starting %s" % mp.current_process().name def workers(self): homecoming self.pool._pool[:] q = queue() w = workers() class index: def get(self): output = { "action": "status", "queue_size": q.size(), "workers_size": len(w.workers()), "workers": "{0}".format(w.workers()) } homecoming json.dumps(output) def post(self): jobid = "jobid:{%s}" % str(random.random()) print jobid i,x in enumerate(range(20)): q.put([i,x]) print "putting %s, %s" % (i, x) if __name__ == "__main__": app = web.application(urls, globals()) app.run()
python multiprocessing daemon web.py
Comments
Post a Comment