开发者

Python subprocess.call weird behavior with multiple calls

开发者 https://www.devze.com 2023-01-06 19:26 出处:网络
I am trying to call remote (ssh) commands using the subprocess.call function like this. import shlex from subprocess import call

I am trying to call remote (ssh) commands using the subprocess.call function like this.

import shlex
from subprocess import call
cmd1='ssh user@example.com mkdir temp'
cmd2='scp test.txt user@example.com:temp'

call(shlex.split(cmd1))
call(shlex.split(cmd2))

When I call the above, the mkdir does not seem to execute - although the documentation for subprocess.call says it waits开发者_JAVA百科 for execution before returning. The latency of the individual ssh calls is about 0.5 seconds. It seems to work fine on the gigabit LAN where the latency is almost zero.

However it seems to work fine when the calls are made like this:

call(shlex.split(cmd1)) &  call(shlex.split(cmd2))

What is the problem with the first approach?

Thank you, Miliana


It looks like you don't look for the result of call in the first method.

if call(shlex.split(cmd1))!=0:
    call(shlex.split(cmd2))


Your problematic version always works for me. I would think this is a network problem especially since you indicate that it works in gigabit LANs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号