Popen 在中创建一个附加进程venv
如果我使用 sys.executable。
请参阅以下脚本:
# parent.py
import time
import os
import sys
import psutil
def print_process(proc=None, depth=0):
if proc is None:
proc = psutil.Process(os.getpid())
print(' ' *depth + str(proc.pid))
children = proc.children()
for ch in children:
if ch.pid == proc.pid:
break
print_process(ch, depth+2)
def startChild():
python_executable = sys.executable
# python_executable = 'python' # UNCOMMENT this line
commandLine = [python_executable, 'child.py']
print(f'Child starter command: {commandLine}')
app = psutil.Popen(commandLine)
time.sleep(.1)
child_pid = app.pid
print(f"PARENT: Child's pid: {child_pid}")
if __name__ == '__main__':
print(f'PARENT started! PID: {os.getpid()}')
startChild()
print('PARENT: pid tree:')
print_process()
time.sleep(1)
# child.py
import os
import time
print(f'CHILD started! PID: {os.getpid()}')
time.sleep(1)
Run parent.py
:
(stack_venv) D:\stack>parent.py
PARENT started! PID: 18068
CHILD has started! PID: 13748 <--- This is weird
PARENT: Child's pid: 19460 <--- This is weird
PARENT: pid tree:
18068
19460
13748
问题是孩子和父母看到孩子的 pid 不同。
重现需要以下内容:
- 在 python3 中运行
venv
(不在遗产中virtualenv
)
- 给孩子打电话
sys.executable
- 使用 Windows(我无法在 WSL 上重现)
以下内容不予转载:
- 子流程创建的包。您可以使用
win32process
or subprocess
I use:
- Windows 10.0.19041 内部版本 19041
- Python 3.7.5
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)