Re: Persistent Pyro
Mayavimmer <[email protected]> Tue, 08 Jul 2014 16:20:28 +0200
| Newsgroups | gmane.comp.python.pyro |
|---|---|
| Message-ID | <[email protected]> |
Success! (almost). I figured out most of the knots including the big one. Here it is (drum roll): to persist a Pyro object with Shelve all you have to do is unregister it first! I am including the last version. I have yet to deal with the client crashing on server shutdown. I could trap the "ConnectionClosedError: receiving: not enough data", I suppose. I intend to clean this up and publish it on Stackoverflow or something, especially if I figure out the client shutdown issue. Amazingly there seem to be NO examples of this on the vast internet! Of course this would work even better as Phase4 of the Warehouse example. ------------------------------------------------------------------------------ Open source business process management suite built on Java and Eclipse Turn processes into business applications with Bonita BPM Community Edition Quickly connect people, data, and systems into organized workflows Winner of BOSSIE, CODIE, OW2 and Gartner awards http://p.sf.net/sfu/Bonitasoft _______________________________________________ Pyro-core mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/pyro-core
xx_warehouse.py
(text/x-python, 2 KB)
from __future__ import print_function
import Pyro4
import person
from pdb import set_trace as ddd
MASTER_HOST = "192.168.56.1"
import shelve
import sys
dmon = Pyro4.Daemon( host=MASTER_HOST )
ns = Pyro4.naming.locateNS()
class Warehouse(object):
def __init__(self):
self.contents = ["chair", "bike", "flashlight", "laptop", "couch"]
# dup() is required to get a non-Pyro copy to save!
def dup( _ ):
n = Warehouse()
n.contents[:] = _.contents
return n
def quit( _ ):
print( "QUIT" )
ns.remove( 'example.warehouse' )
dmon.unregister( _ )
### n = _.dup() # cannot save Pyroized instance
### n.save()
_.save()
dmon.shutdown()
sys.exit(0)
def save( _ ):
print( "SAVING!" )
reg = shelve.open("yy.shelve")
reg['warehouse'] = _
reg.close()
def list_contents(self):
return self.contents
def take(self, name, item):
self.contents.remove(item)
print("{0} took the {1}.".format(name, item))
def store(self, name, item):
if item == 'quit':
self.quit()
self.contents.append(item)
print("{0} stored the {1}.".format(name, item))
def doStuff( _ ):
print( "doStuff" )
import os
os.system("hostname")
def main():
### warehouse = Warehouse()
reg = shelve.open("yy.shelve")
# uncomment to clean db
# del reg['warehouse']; reg.close(); sys.exit( 0 )
if 'warehouse' in reg:
print( "LOADING WAREHOUSE FROM SHELVE" )
warehouse = reg['warehouse']
else:
print( "CREATING NEW WAREHOUSE" )
warehouse = Warehouse()
# reg['warehouse'] = warehouse
reg.close()
### uri = dmon.register( warehouse )
# example.warehouse --> PYRO:obj_e251f3585c364d57944ad40f203e7d2f-Q0ErXNX1RuYSzy4p5JnEaw@public.gmane.org:46280
uri = dmon.register( warehouse )
print( "uri: %s" % uri )
ns.register( "example.warehouse", uri )
### ns.register( "[email protected]", uri )
dmon.requestLoop()
if __name__=="__main__":
main()