首页 文章

如何运行Python的子进程并将其保留在后台

提问于
浏览
1

我看过很多关于我话题的帖子,但实际上我找不到解决问题的方法 . 我正在尝试在后台运行子进程,而不等待子进程执行 . 被调用的子进程是一个shell脚本,它执行许多不同的操作 . 这是我的一小段代码:

print "Execute command:", full_command
subprocess.Popen(full_command, stdin=None, stdout=None, stderr=None, close_fds=True).communicate()
print "After subprocess"

我的问题是Python等到subprocess.Popen完成它的工作 . 我读过,stdin(-out,-err)=无应该解决这个问题,但事实并非如此 . 另外close_fds = True,在这里没有用 .

1 回答

  • 7

    来自Popen.communicate documentation(强调我的):

    与流程交互:将数据发送到stdin . 从stdout和stderr读取数据,直到达到文件结尾 . 等待进程终止 . 可选的输入参数应该是要发送到子进程的字符串,如果没有数据应该发送给子进程,则应为None .

    如果你没有't want to wait for the process to terminate, then simply don'来电话 communicate

    subprocess.Popen(full_command, close_fds=True)
    

相关问题