在Python中运行后台进程,不要等待

har*_*lle 10 python subprocess popen background-process

我的目标很简单:启动rsync并且不要等待.

Debian上的Python 2.7.9

示例代码:

rsync_cmd = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1)
rsync_cmd2 = "/usr/bin/rsync -a -e 'ssh -i /home/myuser/.ssh/id_rsa' {0}@{1}:'{2}' {3} &".format(remote_user, remote_server, file1, file1)
rsync_path = "/usr/bin/rsync"
rsync_args = shlex.split("-a -e 'ssh -i /home/mysuser/.ssh/id_rsa' {0}@{1}:'{2}' {3}".format(remote_user, remote_server, file1, file1))
#subprocess.call(rsync_cmd, shell=True)     # This isn't supposed to work but I tried it
#subprocess.Popen(rsync_cmd, shell=True)    # This is supposed to be the solution but not for me
#subprocess.Popen(rsync_cmd2, shell=True)   # Adding my own shell "&" to background it, still fails
#subprocess.Popen(rsync_cmd, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)  # This doesn't work
#subprocess.Popen(shlex.split(rsync_cmd))   # This doesn't work
#os.execv(rsync_path, rsync_args)           # This doesn't work
#os.spawnv(os.P_NOWAIT, rsync_path, rsync_args) # This doesn't work
#os.system(rsync_cmd2)                      # This doesn't work
print "DONE"
Run Code Online (Sandbox Code Playgroud)

(我已经注释掉执行命令只是因为我实际上在我的代码中保留了所有的试验,以便我知道我做了什么以及我做了什么.显然,我会用正确的方式运行脚本行未注释.)

这会发生什么......我可以在服务器上观看传输,一旦完成,我就会在屏幕上打印"DONE".

我希望发生的是在发出rsync命令后立即打印"DONE" 并开始传输.

似乎非常直截了当.我已经按照其他帖子中列出的细节,比如这个这个,但有些东西阻止它为我工作.

提前谢谢.

(我已经尝试了我在StackExchange中可以找到的所有东西,并且不觉得这是重复的,因为我仍然无法让它工作.在我的设置中有些东西是不对的,需要帮助.)

Via*_*kyi 16

以下是Python REPL的验证示例:

>>> import subprocess
>>> import sys
>>> p = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(100)'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT); print('finished')
finished
Run Code Online (Sandbox Code Playgroud)

如何通过另一个终端窗口验证:

$ ps aux | grep python
Run Code Online (Sandbox Code Playgroud)

输出:

user           32820   0.0  0.0  2447684   3972 s003  S+   10:11PM   0:00.01 /Users/user/venv/bin/python -c import time; time.sleep(100)
Run Code Online (Sandbox Code Playgroud)

  • @harperville 我觉得行为的原因在于你的命令,它可能等待来自`stdin`的一些输入。 (2认同)

jfs*_*jfs 8

Popen()启动一个子进程 - 它不会等待它退出..wait()如果要等待子进程,则必须显式调用方法.从这个意义上讲,所有子进程都是后台进程.

另一方面,子进程可以从父进程继承各种属性/资源,例如打开文件描述符,进程组,其控制终端,一些信号配置等 - 它可能导致阻止祖先进程退出,例如,Python子进程.check_call vs .check_output或者孩子可能会在Ctrl-C上过早死亡(SIGINT信号被发送到前台进程组)或者终端会话被关闭(SIGHUP).

要完全取消子进程,您应该将其作为守护进程.有时候介于两者之间的东西就足够了,例如,它足以将继承的stdout重定向到孙子中,这样.communicate()父母就可以在其直接子进入时返回.