简体   繁体   English

如何等到用户关闭python中的进程?

[英]How to wait until user close the process in python?

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')

Popen has this built in using the poll method. Popen使用poll方法内置了这个。

from subprocess import Popen

process = Popen('C:\Windows\System32\calc')
process.wait()

while not process.poll():
    print('Process running')

print('Done')

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

相关问题 Python - 如何在不设置计时器的情况下等到 function 完成进程 - Python - How to wait until function finishes process without setting a timer 等待直到使用Python WMI完成过程 - Wait until process completes using Python WMI 如何在进程终止后等待,直到使用Python在Windows上将文件解锁为止? - How to wait after process kill until the file is unlocked on Windows using Python? Python:生成sudo进程(在新终端中),等待完成 - Python: Spawn sudo process (in new terminal), wait until completed 如何使Excel RefreshAll等待关闭直到完成? - How do I make an Excel RefreshAll wait to close until finished? java在关闭输入流之前无法读取python进程的输出 - java could not read output of python process until close the input stream Python:如何等待子进程完成初始化 - Python: How to wait until a subprocess finishes initializing 如何等到你在python中释放按钮 - How to wait until you release a button in python 在python中,在Windows上,如何等待鼠标移动? - In python, on windows, how to wait until mouse moves? 如何使用某些参数打开.exe文件并等待用户关闭命令提示符以继续执行Python中的代码 - How to open a .exe file with some parameters and wait to the user close the Command Prompt to continue the code in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM