Python:code.interact(local = locals())其中stdin / stdout不可用

时间:2021-05-19 02:11:49

In Python, the following code snippet will open an interactive shell upon execution.

在Python中,以下代码片段将在执行时打开交互式shell。

import code; code.interact(local = locals())

This has proved tremendously useful for debugging in quite a bit of code that is poorly documented. One can use the shell to navigate the in-program environment and figure out what's going on, even without a debugger. So far, so good.

事实证明,这对于在相当多的文档中进行调试非常有用。即使没有调试器,也可以使用shell来导航程序内环境并找出正在发生的事情。到现在为止还挺好。

Now the challenge.

现在是挑战。

The software I'm using (which is written in Django, by the way) uses some sort of scheduling mechanism which then speaks to another Python process, to which I have no control over other than editing its code. I have no input to it other than the variables I send to it for processing.

我正在使用的软件(顺便说一下,用Django编写)使用某种调度机制,然后说明另一个Python进程,除了编辑代码之外我无法控制。除了我发送给它进行处理的变量之外,我没有任何输入。

However, I don't know how the code works since documentation is very poor, so I wanted to use the code.interact method to figure things out.

但是,我不知道代码是如何工作的,因为文档非常差,所以我想使用code.interact方法来解决问题。

But this process is started somewhere in the background by some special scheduling software, so the flow does not go from the Django application to the portions I wish to examine. Instead, a signal and object are sent which are then run later, at an arbitrary time (anywhere between 10-200ms) in a completely different process. When the signal and object are received, stdin/stdout is out of the picture altogether.

但是这个过程是通过一些特殊的调度软件在后台的某个地方启动的,所以流程不会从Django应用程序转到我想要检查的部分。相反,在完全不同的过程中,发送信号和对象,然后在任意时间(10-200ms之间的任何时间)运行。当接收到信号和对象时,stdin / stdout完全不在图片中。

So I figured that instead of using stdin/stdout to communicate with code.interact I could use a file handle or Unix socket by specifying the readfunc parameter. I've tried this by open()ing a file and socket, but to no avail.

所以我认为不是使用stdin / stdout与code.interact通信,而是通过指定readfunc参数来使用文件句柄或Unix套接字。我试过open()一个文件和套接字,但无济于事。

Now I'm trying to get that to work merely from the Django process itself so even the scheduling problem is out of the question, and while the interactive shell indeed starts, it shuts down immediately, neither accepting a file with commands as content, nor a Unix socket to which Python commands are piped.

现在我试图仅仅从Django进程本身开始工作,所以即使调度问题也是不可能的,当交互式shell确实启动时,它立即关闭,既不接受带有命令作为内容的文件,也不接受用于Python命令的Unix套接字。

To make a long story short; is it possible to communicate with an interactive shell invoked by code.interact by some other means than stdin/stdout? If so, how?

使长话短说;是否可以通过除stdin / stdout之外的其他方式与code.interact调用的交互式shell进行通信?如果是这样,怎么样?

Thanks in advance.

提前致谢。

1 个解决方案

#1


4  

I don't entirely follow the bit about the scheduler and django and whatever.

我不完全关注调度程序和django等等。

But to answer the core of your question:

但要回答你问题的核心:

#!/usr/bin/python

import code

f = open('input.txt', 'r')

def readfunc(prompt):
    return f.readline()

code.interact(readfunc=readfunc)

Then run that in one terminal:

然后在一个终端运行:

$ ./test.py 
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

Notice you don't get a prompt.

请注意,您没有收到提示。

Then in another terminal run:

然后在另一个终端运行:

echo "globals()" >> input.txt

And back in the first terminal you'll see the output.

回到第一个终端,你会看到输出。

#1


4  

I don't entirely follow the bit about the scheduler and django and whatever.

我不完全关注调度程序和django等等。

But to answer the core of your question:

但要回答你问题的核心:

#!/usr/bin/python

import code

f = open('input.txt', 'r')

def readfunc(prompt):
    return f.readline()

code.interact(readfunc=readfunc)

Then run that in one terminal:

然后在一个终端运行:

$ ./test.py 
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)

Notice you don't get a prompt.

请注意,您没有收到提示。

Then in another terminal run:

然后在另一个终端运行:

echo "globals()" >> input.txt

And back in the first terminal you'll see the output.

回到第一个终端,你会看到输出。