1

I'm trying to understand how fabric is working with multiple commands on one machine. I need to run several commands on each host (thousands of hosts) and would like to know what would be best.

Using multiple runs:

res_1 = run(command_1)
res_2 = run(command_2)
...
...
res_n = run(command_n)

Or:

res = run(command_1 && command_2 && ... command_n)
res.splitlines()
res_1 = res[0]
res_2 = res[1]
...
...
res_n = res[n-1]

What I want to know is how fabric handles multiple runs, will it open multiple sessions or do all commands in the same session?

2 Answers 2

5

Regardless of whether you use multiple run calls or a single run call with &&, AFAIK, fabric will open only one network connection. The difference between the two is that each new run executes in a different environment. For example you can try this.

run('ls')
run('cd /tmp/')
run('ls')

Both times it will show you a listing of your home directory. But if you try this

run('ls')
run('cd /tmp/ && ls')

It will show you your home directory the first time, followed by a listing of /tmp/. Thus if you want the state to be preserved from one command to another you should do run('cmd1 && cmd1') but if you are not bothered about it, you should use multiple run calls.

Sign up to request clarification or add additional context in comments.

2 Comments

My commands are independent of each other so it is recommended to use multiple runs?
Yes, I would use multiple runs if nothing else. they are more readable.
1

The important difference between those two approaches is that the first one will run all commands regardless of the exit status of former commands; the second one, however, will only execute commmand_2 if command_1 returned no error, etc. So it is up to you if you want this behaviour or the other.

3 Comments

Hang on a sec, unless I am very much mistaken, if any of the commands return non zero exit states, fabric will not run the rest regardless of whether you use style 1 or style 2
I stand corrected - unless warn_only is set to True, it will behave as you said.
What I'm trying to understand is if using multiple run will cause more "network overhead" then using one run to execute multiple commands

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.