backdoor——正在运行的进程中的 Python 交互式解释器

backdoor -- Python interactive interpreter within a running process

backdoor 模块便于检查长时间运行进程的状态。它提供了一个正常的 Python 交互式解释器,同时不会阻塞应用程序的正常运行。这在调试、性能调优或简单地观察系统实际行为时非常有用。

在应用程序中,使用一个 greenthread 在监听套接字上运行 backdoor_server 进行启动:

eventlet.spawn(backdoor.backdoor_server, eventlet.listen(('localhost', 3000)), locals())

当这个服务运行时,可以通过 telnet 连接指定端口访问 backdoor。

$ telnet localhost 3000
(python 版本,构建信息)
输入 "help""copyright""credits"  "license" 以了解更多信息。
>>> import myapp
>>> dir(myapp)
['__all__', '__doc__', '__name__', 'myfunc']
>>>

backdoor 会在命令间隔中协作式地让出控制权,使应用程序的其余部分得以继续运行。因此,在持续服务请求的服务器上,您可以在解释器命令之间观察内部状态的变化。

The backdoor module is convenient for inspecting the state of a long-running process. It supplies the normal Python interactive interpreter in a way that does not block the normal operation of the application. This can be useful for debugging, performance tuning, or simply learning about how things behave in situ.

In the application, spawn a greenthread running backdoor_server on a listening socket:

eventlet.spawn(backdoor.backdoor_server, eventlet.listen(('localhost', 3000)), locals())

When this is running, the backdoor is accessible via telnet to the specified port.

$ telnet localhost 3000
(python version, build info)
Type "help", "copyright", "credits" or "license" for more information.
>>> import myapp
>>> dir(myapp)
['__all__', '__doc__', '__name__', 'myfunc']
>>>

The backdoor cooperatively yields to the rest of the application between commands, so on a running server continuously serving requests, you can observe the internal state changing between interpreter commands.

class eventlet.backdoor.SocketConsole(desc, hostport, locals)
switch(*args, **kwargs)

Switch execution to this greenlet.

If this greenlet has never been run, then this greenlet will be switched to using the body of self.run(*args, **kwargs).

If the greenlet is active (has been run, but was switch()'ed out before leaving its run function), then this greenlet will be resumed and the return value to its switch call will be None if no arguments are given, the given argument if one argument is given, or the args tuple and keyword args dict if multiple arguments are given.

If the greenlet is dead, or is the current greenlet then this function will simply return the arguments using the same rules as above.

eventlet.backdoor.backdoor(conn_info, locals=None)

Sets up an interactive console on a socket with a single connected client. This does not block the caller, as it spawns a new greenlet to handle the console. This is meant to be called from within an accept loop (such as backdoor_server).

eventlet.backdoor.backdoor_server(sock, locals=None)

Blocking function that runs a backdoor server on the socket sock, accepting connections and running backdoor consoles for each client that connects.

The locals argument is a dictionary that will be included in the locals() of the interpreters. It can be convenient to stick important application variables in here.