I have a Pyro4 distributed system with multiple clients connecting to a single server. These clients connect to a remote object, and that object may allocate some resources in the system (virtual devices, in my case).
Once a client disconnects (let's say because of a crash), I need to release those resources. What is the proper way to detect that an specific client has disconnected from an specific object?
I've tried different things:
- Overriding the
Daemon.clientDisconnected
method. I get aconnection
parameter from this method. But I can't correlate that to an object, because I have no access to which remote object that connection refers to. - Using
Pyro4.current_context
inDaemon.clientDisconnected
. This doesn't work because that is a thread-local object. That in place, if I have more clients connected than threads in my pool, I get repeated contexts. - Using
Proxy._pyroAnnotations
as in the "usersession" example available by the Pyro4 project, doesn't help me, because again, I get the annotation from thePyro4.core.current_context.annotations
attribute, which shows me wrong annotations whenDaemon.clientDisconnected
is called (I imagine due to a thread related issues). - Using
instance_mode="session"
and the__del__
method in the remote class (as each client would have a separate instance of the class, so the instance is supposed to be destroyed once the client disconnects). But this relies on the__del__
method, which has some problems as some Python programmers would point out.
I added my current solution as an answer, but I really would like to know if there's a more elegant way of doing this with Pyro4, as this scenario is a recurrent pattern in network programming.