如何在使用os.execl替换进程时重定向stdin / stdout / stderr

时间:2022-05-25 21:02:29

Consider the following sample script:

请考虑以下示例脚本:

import os
import sys

print(1)
os.execl(sys.executable, sys.executable, '-c', 'print(2)')
print(3)

The result is

结果是

1

I was expecting

我在期待

1
2

I think it is because the replacing process is not using the same stdin/stdout/stderr?

我认为这是因为替换过程不使用相同的stdin / stdout / stderr?

How can I achieve what I was expecting for while using execl?

在使用execl时,如何实现我期望的效果?

I'm using Python 3.6 on Windows.

我在Windows上使用Python 3.6。

2 个解决方案

#1


2  

This is not a bug about PyCharm as I cannot reproduce it with IDEA too. IDEA is using the same core as PyCharm using.

这不是关于PyCharm的错误,因为我无法用IDEA重现它。 IDEA正在使用与PyCharm相同的核心。

This is because of the way you launch your script. If you launch your script with Run, it works. If you launch it with Debug, it doesn't.

这是因为您启动脚本的方式。如果使用Run启动脚本,则可以正常运行。如果使用Debug启动它,则不会。

Because Run just run script in a terminal, but Debug will launch a debugger and connect that process to this debugger. The output you see is actually from debugger but not directly from your script. When you replace your process, debugger won't rebuild connection to that new-created process.

因为Run只是在终端中运行脚本,但Debug将启动调试器并将该进程连接到此调试器。您看到的输出实际上来自调试器,但不是直接来自您的脚本。替换进程时,调试器不会重建与新创建进程的连接。

That's why you didn't get 2 outputted.

这就是为什么你没有得到2输出。

#2


1  

In Linux,there is a flag FD_CLOEXEC,you can test it by fcntl.fcntl(sys.stdout,fcntl.F_GETFD)

在Linux中,有一个标志FD_CLOEXEC,您可以通过fcntl.fcntl(sys.stdout,fcntl.F_GETFD)对其进行测试

the behavior you described can reproduce in ubuntu16 by

你描述的行为可以在ubuntu16中重现

import os
import sys
import fcntl
print(1)
ret = fcntl.fcntl(sys.stdout, fcntl.F_GETFD)
ret |= fcntl.FD_CLOEXEC
fcntl.fcntl(sys.stdout, fcntl.F_SETFD, ret)
os.execl(sys.executable, sys.executable, '-c', 'print(2)')
print(3)

So when you run in PyCharm,it must redirect the stdout and set the equivalent windows flag.

因此,当您在PyCharm中运行时,它必须重定向标准输出并设置等效的Windows标志。

#1


2  

This is not a bug about PyCharm as I cannot reproduce it with IDEA too. IDEA is using the same core as PyCharm using.

这不是关于PyCharm的错误,因为我无法用IDEA重现它。 IDEA正在使用与PyCharm相同的核心。

This is because of the way you launch your script. If you launch your script with Run, it works. If you launch it with Debug, it doesn't.

这是因为您启动脚本的方式。如果使用Run启动脚本,则可以正常运行。如果使用Debug启动它,则不会。

Because Run just run script in a terminal, but Debug will launch a debugger and connect that process to this debugger. The output you see is actually from debugger but not directly from your script. When you replace your process, debugger won't rebuild connection to that new-created process.

因为Run只是在终端中运行脚本,但Debug将启动调试器并将该进程连接到此调试器。您看到的输出实际上来自调试器,但不是直接来自您的脚本。替换进程时,调试器不会重建与新创建进程的连接。

That's why you didn't get 2 outputted.

这就是为什么你没有得到2输出。

#2


1  

In Linux,there is a flag FD_CLOEXEC,you can test it by fcntl.fcntl(sys.stdout,fcntl.F_GETFD)

在Linux中,有一个标志FD_CLOEXEC,您可以通过fcntl.fcntl(sys.stdout,fcntl.F_GETFD)对其进行测试

the behavior you described can reproduce in ubuntu16 by

你描述的行为可以在ubuntu16中重现

import os
import sys
import fcntl
print(1)
ret = fcntl.fcntl(sys.stdout, fcntl.F_GETFD)
ret |= fcntl.FD_CLOEXEC
fcntl.fcntl(sys.stdout, fcntl.F_SETFD, ret)
os.execl(sys.executable, sys.executable, '-c', 'print(2)')
print(3)

So when you run in PyCharm,it must redirect the stdout and set the equivalent windows flag.

因此,当您在PyCharm中运行时,它必须重定向标准输出并设置等效的Windows标志。