Example #1
0
    def __init__(self, timeout, nshost, debug):

        Pyro.core.ObjBase.__init__(self)

        self.debug = debug
        self.nshost = nshost
        self.instance_uri = None
        self.host = None

        #fork a WatchDog thread, which sleeps all the time and quits if it
        #hasn't been pinged after timeout minutes, by calling self.watchdog.set(xxx)
        #where xxx is a float (the current time).
        self.watchdog = WatchDog(timeout, debug, logfile='watchdog.debug')
        self.watchdog.start()
Example #2
0
class PyroHandler(Pyro.core.ObjBase):
    """
    Runs on remote side, non-specific object.

    It is a tool to launch instances of any picklable object
    on the remote host (where it resides)

    If PyroHandler is idle longer than .timeout time, it kills itself
    on the remote side (together with all registered instances)

    Returns: raw Pyro proxy

    """

    def __init__(self, timeout, nshost, debug):

        Pyro.core.ObjBase.__init__(self)

        self.debug = debug
        self.nshost = nshost
        self.instance_uri = None
        self.host = None

        #fork a WatchDog thread, which sleeps all the time and quits if it
        #hasn't been pinged after timeout minutes, by calling self.watchdog.set(xxx)
        #where xxx is a float (the current time).
        self.watchdog = WatchDog(timeout, debug, logfile='watchdog.debug')
        self.watchdog.start()

    def set(self, time):
        self.watchdog.set(time)

    def publish(self, instance, instance_uri, delegate):
        """
        delegate = False for descendants of Pyro.core.ObjBase

        Publishes a copy of the instance on the host where PyroHandler
        is running.

        """

        if self.debug:
            print 'PyroHandler.publish: publishing instance %s with URI %s' % (instance, instance_uri)

        self.t = threading.Thread(target = PyroUtils.launch_instance, \
                        args = (instance, instance_uri, delegate,
                                self.nshost, self.debug, False))
        self.t.start()

        self.instance_uri = instance_uri

        ## to enable cleaning up upon remote shutdown

        atexit.register(self.terminate)

    def terminate(self):

        ## called from PyroProxy.terminate or on sys.exit (WatchDog call)

        if self.debug:
            print 'PyroHandler.terminate: %s terminating... ' % self

        ##--- call terminate only
        ## if the handler is already terminated (e.g. by hands)
        ## then function is not called on the exit

        topop = [atexit._exithandlers[i][0] == self.terminate
                 for i in range(len(atexit._exithandlers))]
        if True in topop:
            atexit._exithandlers.pop( topop.index(True) )

        ##--- unregister from the nameserver
        ## it will waits PyroUtils.default_timeout to let it
        ## finish self-unregistration

        if not self.instance_uri == None:
            PyroUtils.unregister(self.instance_uri, ns = self.nshost)

        ##--- shuts down remote process
        ## will leave interpreter alive if debug = True

        ## PyroHandler itself is an object instance managed
        ## by Pyro.core.Daemon, hence will be stopped by setting
        ## PyroHandler._pyro_stop to True

        self._pyro_stop = True

        ## PyroUtils.is_stopped(self._pyro_suppl['uri'], ns = self.nshost)
        ##
        ## but also it may cause problems when running without xterm  (unconfirmed)
        ## because it shuts down the python process (daemon.shutdown()) causing
        ## connection lost error

    def __str__(self):

        if hasattr(self, '_pyro_suppl'):
            s = '%s(uri="%s", host="%s")' % (self.__class__.__name__, \
                self._pyro_suppl['uri'], self._pyro_suppl['host'])
        else:
            s = self.__class__.__name__

        return s