Skip to content

Instantly share code, notes, and snippets.

@bcatubig
Created December 19, 2016 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcatubig/5a9b430bb49ccc45d611bc347bd13226 to your computer and use it in GitHub Desktop.
Save bcatubig/5a9b430bb49ccc45d611bc347bd13226 to your computer and use it in GitHub Desktop.
Python Bash Multiprocessing
import subprocess
ps = []
def main():
for i in range(1,5):
p = subprocess.Popen("ansible all -i localhost, -c local -a 'date'", shell=True)
ps.append(p)
while True:
ps_status = [p.poll() for p in ps]
if all([x is not None for x in ps_status]):
break
print("[+] Done Processing")
if __name__ == '__main__':
main()
@bcatubig
Copy link
Author

bcatubig commented Dec 19, 2016

POC for running multiple bash commands in parallel

similiar to

ansible all -i localhost, -c local -a 'date' &

Output

~/python python process.py
localhost | SUCCESS | rc=0 >>
Mon Dec 19 03:23:48 UTC 2016

localhost | SUCCESS | rc=0 >>
Mon Dec 19 03:23:48 UTC 2016

localhost | SUCCESS | rc=0 >>
Mon Dec 19 03:23:48 UTC 2016

localhost | SUCCESS | rc=0 >>
Mon Dec 19 03:23:48 UTC 2016

[+] Done Processing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment