irmen / Pyro4

Pyro 4.x - Python remote objects
http://pyro4.readthedocs.io/
MIT License
715 stars 83 forks source link

Pyro4 instance_mode="single" but creating multiple instances of the object #230

Closed sooryranga closed 4 years ago

sooryranga commented 4 years ago

I registered an object with :

@Pyro4.expose
@Pyro4.behavior(instance_mode="single")
class Animals(object):
    def __init__(self):
        print("Called")

Called it with serveSimple:

Pyro4.Daemon.serveSimple(
        {Animals: PYRO_ANIMAL_APP_NAME},
        ns=True,
        verbose=False,
        host="localhost",
    )

This results in :

called
called

Does pyro4 expects animal object to be a singleton? or is it taken care of when we specify instance_mode="single"?

irmen commented 4 years ago

No, the instance mode 'single' makes Pyro take care to create the object only once.

Your output is not reproducible behavior (I see just one "Called" being printed at all times) There has to be something else going on in your code. You're probably creating an instance of Animals somewhere else in your code.

What Pyro version are you using? Have you tried the 'instancemode' examples that come with Pyro?

sooryranga commented 4 years ago

I do have a Gunicorn which tries to access animals instance :

workers = min(cpu_count(), 4)
timeout = 300
preload_app = True

def on_starting(server):
    animals = Proxy("PYRONAME:" + PYRO_DECODER_APP_NAME)

    while True:
        try:
            animals.wait_till_ready()
            break
        except NamingError:
            pass

I have : Pyro4 (4.78)

Since there is no lock on: https://github.com/irmen/Pyro4/blob/9fd891c1cf33530cd263f83ed7f805317474a48c/src/Pyro4/core.py#L1535-L1543

Wouldn't there be a chance that multiple calls to pyro-ns result in multiple initialization of animals if animals take a long time to be created ( ie init in animals take a long time) ?

irmen commented 4 years ago

Hmm. If this proves to be an issue, I will perhaps not fix it in Pyro4 but only in Pyro5.

irmen commented 4 years ago

This was a trivial fix actually, thanks for reporting