在 Python 中运行一个没有父进程的子进程 [英] Run a child process free of the parent in Python

查看:94
本文介绍了在 Python 中运行一个没有父进程的子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序正在另一个程序中运行.父程序在子进程运行时冻结.有没有办法在操作系统中将子进程作为父进程本身运行?

I have a program I'm running within another. The parent program freezes up while the child process is running. Is there a way to run the child process in the OS as a parent process itself?

推荐答案

您可以使用 subprocess.Popen,假设您真的想启动一个与父 Python 脚本完全分离的程序:

You can use subprocess.Popen, assuming you're really trying to launch a program that's completely separate from the parent Python script:

import subprocess
subprocess.Popen(["command", "-a", "arg1", "-b", "arg2"])

这将启动 command 作为调用脚本的子进程,不会阻塞等待它完成.如果父进程退出,子进程将继续运行.

This will launch command as a child process of the calling script, without blocking to wait for it to finish. If the parent exits, the child process will continue to run.

这篇关于在 Python 中运行一个没有父进程的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆