Re: Persistent Pyro

Mayavimmer <[email protected]> Tue, 08 Jul 2014 00:30:57 +0200
Newsgroups gmane.comp.python.pyro
Message-ID <[email protected]>
What I would like to do is to get ANY example of Pyro persistence 
working. I tried lots of different things but always ran into a 
different snag. I also googled for a simple example I could copy but did 
not find any. It would appear that nobody has used Pyro with 
persistence! If I ever get to make this work I'll put up a simple hello 
world like demo on Stackoverflow or something to save people time.

The closest I got to a working demo is by adapting the warehouse 
example. I am including the three files:

     1. person.py is unchanged
     2. yy_visit.py is visit.py with an IP added
     3. yy_warehouse.py has the most changes.

As it now stands, the demo does save to the shelve file and it does show 
the new content when restarted. However I cannot figure out how to 
shutdown cleanly. Currently entering 'quit' instead of an item name 
triggers the quit function.

Also it would be nice to be able to save the Pyro object directly into 
the Shelve. However when you read the Pyro object back from the Shelve 
and try to publish it, it errors out with a message about already having 
an id, naturally. Ideally I should be able to read the previously stored 
Pyro object, and erase all the added on metadata before feeding it to 
the daemon for serving.

------------------------------------------------------------------------------
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
person.py (text/x-python, 878 B)
from __future__ import print_function
import sys

if sys.version_info < (3, 0):
    input = raw_input


class Person(object):
    def __init__(self, name):
        self.name = name
        
    def visit(self, warehouse):
        print("This is {0}.".format(self.name))
        self.deposit(warehouse)
        self.retrieve(warehouse)
        print("Thank you, come again!")
        
    def deposit(self, warehouse):
        print("The warehouse contains:", warehouse.list_contents())
        item = input("Type a thing you want to store (or empty): ").strip()
        if item:
            warehouse.store(self.name, item)
            
    def retrieve(self, warehouse):
        print("The warehouse contains:", warehouse.list_contents())
        item = input("Type something you want to take (or empty): ").strip()
        if item:
            warehouse.take(self.name, item)
yy_visit.py (text/x-python, 344 B)
# This is the code that visits the warehouse.
import sys
import Pyro4
import Pyro4.util
from person import Person

sys.excepthook = Pyro4.util.excepthook

warehouse = Pyro4.Proxy("PYRONAME:[email protected]")

janet = Person("Janet")
henry = Person("Henry")
janet.visit(warehouse)
henry.visit(warehouse)
yy_warehouse.py (text/x-python, 1.6 KB)
from __future__ import print_function
import Pyro4
import person

import pdb


MASTER_HOST = "192.168.56.1"


import shelve
import sys


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" )
        n = _.dup()	# cannot save Pyroized instance
        n.save()
        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:
        warehouse = reg['warehouse']
    else:
        warehouse = Warehouse()
	# reg['warehouse'] = warehouse
    reg.close()

    Pyro4.Daemon.serveSimple(
            {
                warehouse: "example.warehouse"
            },

	    ###
	    host = MASTER_HOST,

            ns = True)
            ### ns = False)

if __name__=="__main__":
    main()