简体   繁体   English

Python-处理呼叫并等待

[英]Python - Process call and wait

I got a question about calling a process and wait till this program is closing. 我有一个有关调用进程的问题,请等待直到该程序关闭。

It should work like this: 它应该像这样工作:

Main program and secondary program. 主程序和辅助程序。

The Main program should open the secondary program by clicking on a QPushButton. 主程序应通过单击QPushButton打开辅助程序。

Then the secondary program appears and you can normaly use it. 然后出现辅助程序,您可以正常使用它。

In the moment you close it, ( so the process get killed, by exiting the window) the main program should get terminated too. 在关闭它的那一刻((通过退出窗口,进程被杀死)主程序也应终止。

I know there is the module "subprocess", but I do not understand how to use it. 我知道有模块“子进程”,但是我不知道如何使用它。 The documentation is a bit weired. 该文档有点奇怪。

Thx for help :) 感谢帮助:)

You can try this. 你可以试试看

    import subprocess
    subprocess.call('ping 192.168.132.133')

In Python 3.X, you use subprocess.run() which is preferred. 在Python 3.X中,您首选使用subprocess.run()。 If you want for more advanced cases that run() can not work, you can use popen() directly. 如果您想在更高级的情况下使用run()无效,则可以直接使用popen()。

If you don't need to keep the first program running, you can just close it immediately after opening the second program: 如果不需要保持第一个程序运行,则可以在打开第二个程序后立即将其关闭:

    if QtCore.QProcess.startDetached('prog2', ['arg1', 'arg2']):
        QtWidgets.qApp.quit()
    else:
        print('ERROR: could not start prog2')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM