1

The Linux machine is running a process like given below:

root     18983 18961  0 02:22 pts/0    00:00:04 /xxx/grpcEnv/bin/python /xxx/python/exabgp_shim/ExaBGP_server.py

The python script ExaBGP_server.py basically starts a server and is waiting in an infinite while loop. The script also has some classes with different methods defined.

Question:

How to invoke a class method of the above python script when the process is already running ?

3
  • Ask the script to execute it for you. Commented Apr 7, 2016 at 8:18
  • @sashoalm: I am sorry, but the comment seems too critical. Commented Apr 7, 2016 at 10:23
  • OK, sorry about that. Could you kindly provide the actual code of ExaBGP_server.py? Also, could you provide an example of how you intend to "invoke a method"? Invoke it from where? Commented Apr 7, 2016 at 11:19

2 Answers 2

2

There's no easy way to do that unless you have code in the server to also give you access to an interpreter while it's running, or on launching it.

EDIT: This "manhole" library looks promising for your use case, though like the other ideas listed it does require some modification of the server code to implement.

Is this code you wrote? If so, you could modify it to run the server in a background thread, and then use the code module to launch an interactive interpreter on the console with the appropriate modules/class instances (if needed) accessible. This only works for debugging, since this effectively turns your noninteractive server into an interactive Python process that happens to run a server in the background.

The Twisted library supported a telnet or SSH-based manhole feature, where you could essentially shell into the running Python process at any time, but integrating Twisted into a non-Twisted code base is not worth the trouble for this feature. I think there've been other implementations of this concept, including some that provide fancy HTML/JS interfaces, but I don't have the names/links handy.

Sign up to request clarification or add additional context in comments.

Comments

2

This is exactly the problem that DBus solves. It allows you to invoke methods from another process.

I had asked a question about creating Hello World server using DBus, Register a "Hello World" DBus service, object and method using Python.

This is how the code for the server looks:

import gobject
import dbus
import dbus.service

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)


OPATH = "/com/example/HelloWorld"
IFACE = "com.example.HelloWorld"
BUS_NAME = "com.example.HelloWorld"


class Example(dbus.service.Object):
    def __init__(self):
        bus = dbus.SessionBus()
        bus.request_name(BUS_NAME)
        bus_name = dbus.service.BusName(BUS_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, OPATH)

    @dbus.service.method(dbus_interface=IFACE + ".SayHello",
                         in_signature="", out_signature="")
    def SayHello(self):
        print "hello, world"


if __name__ == "__main__":
    a = Example()
    loop = gobject.MainLoop()
    loop.run()

And you can use dbus-send to call it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.