Communicating with a running python daemon

前端 未结 8 1625
一个人的身影
一个人的身影 2020-12-07 07:45

I wrote a small Python application that runs as a daemon. It utilizes threading and queues.

I\'m looking for general approaches to altering this application so that

相关标签:
8条回答
  • 2020-12-07 08:44

    You could associate it with Pyro (http://pythonhosted.org/Pyro4/) the Python Remote Object. It lets you remotely access python objects. It's easily to implement, has low overhead, and isn't as invasive as Twisted.

    0 讨论(0)
  • 2020-12-07 08:47

    Yet another approach: use Pyro (Python remoting objects).

    Pyro basically allows you to publish Python object instances as services that can be called remotely. I have used Pyro for the exact purpose you describe, and I found it to work very well.

    By default, a Pyro server daemon accepts connections from everywhere. To limit this, either use a connection validator (see documentation), or supply host='127.0.0.1' to the Daemon constructor to only listen for local connections.

    Example code taken from the Pyro documentation:

    Server

    import Pyro.core
    
    class JokeGen(Pyro.core.ObjBase):
            def __init__(self):
                    Pyro.core.ObjBase.__init__(self)
            def joke(self, name):
                    return "Sorry "+name+", I don't know any jokes."
    
    Pyro.core.initServer()
    daemon=Pyro.core.Daemon()
    uri=daemon.connect(JokeGen(),"jokegen")
    
    print "The daemon runs on port:",daemon.port
    print "The object's uri is:",uri
    
    daemon.requestLoop()
    

    Client

    import Pyro.core
    
    # you have to change the URI below to match your own host/port.
    jokes = Pyro.core.getProxyForURI("PYROLOC://localhost:7766/jokegen")
    
    print jokes.joke("Irmen")
    

    Another similar project is RPyC. I have not tried RPyC.

    0 讨论(0)
提交回复
热议问题