如何等到用户关闭Python的过程?
我想创建一个程序,以启动新的流程,然后等到用户或其他程序杀死该过程。
但是我不明白为什么子过程创建的过程是在一小部分时间之后完成的,而不是通过用户关闭流程的。
import subprocess
import psutil
old_process = {p.pid for p in psutil.process_iter()}
calc_process = subprocess.Popen('C:\Windows\System32\calc')
new_process = {p.pid for p in psutil.process_iter()}
process = list(new_process - old_process)[0]
while psutil.pid_exists(process):
print('Process running')
print('Done')
I want to create a program that init a new process and then wait until thhis process was killed by user or another program.
But i cant understand why the process create by subprocess is finished after a fraction of time and not by the user closing the process.
import subprocess
import psutil
old_process = {p.pid for p in psutil.process_iter()}
calc_process = subprocess.Popen('C:\Windows\System32\calc')
new_process = {p.pid for p in psutil.process_iter()}
process = list(new_process - old_process)[0]
while psutil.pid_exists(process):
print('Process running')
print('Done')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
popen
具有使用“ noreferrer”>“ noreferrer”>轮询
方法。Popen
has this built in using thepoll
method.