简体   繁体   English

如何从子处理的python脚本打印到终端窗口

[英]How to print to terminal window from subprocessed python script

I create a subprocess running a python script like this:我创建了一个运行 python 脚本的子进程,如下所示:

p = subprocess.Popen(['nohup', 'python', 'listen.py'],
                     cwd="/execute",
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)

I then let the spawning thread terminate;然后我让生成线程终止; and the spawned subprocess remains running.并且生成的子进程保持运行。 The user (me) is back at the bash terminal.用户(我)回到了 bash 终端。

I wish to print to this terminal from within the subprocess (from listen.py) - how can I achieve this?我希望从子进程内(从 listen.py)打印到这个终端 - 我怎样才能做到这一点?

With nohup , you simply can't.使用nohup ,您根本做不到。 One of the features of nohup is that it detaches the started process from any terminal. nohup的特点之一是它将启动的进程与任何终端分离。

Without nohup , in the trivial case, simply don't use the stdin , stdout , or stderr keyword arguments to Popen() at all;没有nohup ,在一般情况下,根本不使用Popen()stdinstdoutstderr关键字参数; the subprocess will inherit copies of these file descriptors from the calling process.子进程将从调用进程继承这些文件描述符的副本。

Having a background process write to your terminal while you are using it can be extremely disruptive, though;但是,在您使用终端时让后台进程写入终端可能会造成极大的破坏; a better solution is usually to have the process write to a log file, which you can tail -f if you want to, in a different terminal or another program if you like.更好的解决方案通常是让进程写入日志文件,如果您愿意,可以在不同的终端或其他程序中使用tail -f来写入日志文件。

So script A spawns script B and terminates, and then you want to access the stdout of script B?所以脚本 A 产生脚本 B 并终止,然后你想访问脚本 B 的标准输出?

The first thing is that you probably don't want to capture the stdout from script B in A. But the stdout has to go somewhere, such as to a file (solution 2) or it can just be inherited stdout from the parent process (solution 1).第一件事是您可能不想从 A 中的脚本 B 捕获标准输出。但是标准输出必须去某个地方,例如文件(解决方案 2),或者它可以只是从父进程继承标准输出(解决方案1)。

Solution 1解决方案1

The following will exit to the shell immediately then write "123" to the same terminal 3 seconds later.以下将立即退出到 shell,然后在 3 秒后将“123”写入同一个终端。 If you pipe script A to a file then the outputs of script B will go there too.如果您通过管道将脚本 A 传输到文件,那么脚本 B 的输出也会到那里。

python -c "import subprocess; subprocess.Popen(['python', '-c', 'import time; time.sleep(3); print(123)'])"

Solution 2解决方案2

If you want script A to output to the terminal, but for script B's output to be stored somewhere else then you can write it to a file directly, such as by providing the file as the stdout kwarg to Popen like so:如果您希望脚本 A 输出到终端,但要将脚本 B 的输出存储在其他地方,那么您可以将其直接写入文件,例如将文件作为 stdout kwarg 提供给 Popen,如下所示:

p = subprocess.Popen(['python', 'listen.py'],
                     cwd="/execute",
                     stdout=open('/execute/results.txt', 'w'),
                     stderr=subprocess.STDOUT)

Solution 3解决方案3

Or if you specifically want to use nohup then the following will by usually default write the output from the subprocess to nohup.txt或者,如果您特别想使用 nohup 那么下面的内容通常会默认将子进程的输出写入 nohup.txt

p = subprocess.Popen(['nohup', 'python', 'listen.py'],
                     cwd="/execute",
                     stderr=subprocess.STDOUT)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM