os.exec on Windows

强颜欢笑 提交于 2019-12-18 04:46:12

问题


I have a script that calls os.execvp into another Python instance. After doing this, I appear to be attached to a cmd.exe instance, not the Python instance I just created. The Python instance responds to Ctrl+C however.

H:\bin>Python 3.2.1 (default, Jul 10 2011, 21:51:15) [MSC v.1500
 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hi')
Can't find file ('hi')

H:\bin>

H:\bin>
KeyboardInterrupt
>>> echo hi
hi

The call to exec:

from sys import argv
os.execvp('python', argv)

How do I replace the original Python instance with the new one, as per the behaviour one might see on Linux?


回答1:


On Unix executing binaries is split into two stages - fork(3) to clone current process and exec(3) to load executable into address space. On windows there is only CreateProcess which does the same thing as fork+exec.

For portability your best bet is to use subprocess.Popen (which also does proper filename quoting on Windows unlike os.* counterparts) as in http://docs.python.org/library/subprocess.html#replacing-the-os-spawn-family



来源:https://stackoverflow.com/questions/7004687/os-exec-on-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!