Reconnect a running daemon to stdout

473 views Asked by At

I have an object running as a daemon in py3k. For that, I use the Pyro4 module inside a thread (based on the code from Sander Marechal, daemon.py).

class MyDaemon(Daemon):

    def run(self):
        mo = MyObject()
        daemon = Pyro4.Daemon(host=HOST, port=PORT)
        uri = daemon.register(mo, USER)
        logging.debug("MyObject ready. Object uri = {0}".format(uri))

        daemon.requestLoop()

and when needed, I get the object with

mo = Pyro4.Proxy("PYRO:%s@%s:%i" % (USER, HOST, PORT))
mo.myAction(my_args)

Now I want the MyObject module to output text to sdtout. The problem is that, as running in a thread, it is not connected to sys.__stdout__.

class MyObject():
    def greeting(self):
        print("Hello world") # lost

I tried to create a mo.reconnect(sys.__stdout__) function to bind the current stdout to the one in the thread but Pyro4 does not accept buffer as argument.

A solution could be to simply return text at the end of my function which will be recieved by the Pyro4 proxy but I want also to be able to display info inside a function.

The question is also valid for stdin.

Is there a way to achieve what I am looking for ? Is there something I don't get and I'm overcomplicating ? Maybe Pyro4 is not the best way to do that.

Thank you

1

There are 1 answers

1
Toote On BEST ANSWER

Why would you want your daemon to interact with stdin and stdout? The very fact that it is a daemon means that it shouldn't interact with the "user" (for whom stdin and stdout are intended).

Everything depends on what you want to achieve by connecting its input and output to stdin or out:

  • If you want user interaction, you should make your main code act as a proxy to that daemon handling input and output and the daemon just doing the processing. i.e. your daemon's interface should take the input strings (or objects if easier) as parameters and output similar objects that your proxy will take and output to the user.

  • If you want debugging output, a quick patch would be to read directly from the /tmp/sdaemon.log file that is where all the daemon's output goes (according to line 44). A more decent fix would be to implement proper logging throughout your code.