如何运行后台进程并且*不*等待? [英] How to run a background process and do *not* wait?

查看:56
本文介绍了如何运行后台进程并且*不*等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标很简单:启动 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, 远程服务器, 文件 1, 文件 1)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, 文件 1))#subprocess.call(rsync_cmd, shell=True) # 这不应该工作,但我试过了#subprocess.Popen(rsync_cmd, shell=True) # 这应该是解决方案,但不适合我#subprocess.Popen(rsync_cmd2, shell=True) # 添加我自己的 shell "&"背景它,仍然失败#subprocess.Popen(rsync_cmd, shell=True, stdin=None, stdout=None, stderr=None, close_fds=True) # 这不起作用#subprocess.Popen(shlex.split(rsync_cmd)) # 这不起作用#os.execv(rsync_path, rsync_args) # 这不起作用#os.spawnv(os.P_NOWAIT, rsync_path, rsync_args) # 这不起作用#os.system(rsync_cmd2) # 这不起作用打印完成"

(我已经注释掉了执行命令只是因为我实际上将所有试验保留在我的代码中,以便我知道我做了什么和我没有做的事情.显然,我会运行脚本右行未注释.)

发生了什么......我可以在服务器上观看传输,当它完成时,我会在屏幕上打印一个完成".

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

看起来很直接.我遵循了其他帖子中概述的详细信息,例如 this一个和这个,但有些东西阻止它工作对我来说.

提前致谢.

(我已经尝试了我可以在 StackExchange 中找到的所有内容,但我不觉得这是重复的,因为我仍然无法使其正常工作.我的设置中出现了问题,需要帮助.)

解决方案

这是 Python REPL 的验证示例:

<预><代码>>>>导入子流程>>>导入系统>>>p = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(100)'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT);打印('完成')完成的

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

$ ps aux |蟒蛇

输出:

user 32820 0.0 0.0 2447684 3972 s003 S+ 10:11PM 0:00.01/Users/user/venv/bin/python -c 导入时间;时间.睡眠(100)

My goal is simple: kick off rsync and DO NOT WAIT.

Python 2.7.9 on Debian

Sample code:

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"

(I've commented out the execution commands only because I'm actually keeping all of my trials in my code so that I know what I've done and what I haven't done. Obviously, I would run the script with the right line uncommented.)

What happens is this...I can watch the transfer on the server and when it's finished, then I get a "DONE" printed to the screen.

What I'd like to have happen is a "DONE" printed immediately after issuing the rsync command and for the transfer to start.

Seems very straight-forward. I've followed details outlined in other posts, like this one and this one, but something is preventing it from working for me.

Thanks ahead of time.

(I have tried everything I can find in StackExchange and don't feel like this is a duplicate because I still can't get it to work. Something isn't right in my setup and need help.)

解决方案

Here is verified example for 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

How to verify that via another terminal window:

$ ps aux | grep python

Output:

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)

这篇关于如何运行后台进程并且*不*等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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