Created
June 11, 2021 06:22
-
-
Save azalio/0d05b1548d6f8ddf524af12907f06ffc to your computer and use it in GitHub Desktop.
exec shell command in parallel in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cpu_count(): | |
''' Returns the number of CPUs in the system | |
''' | |
num = 1 | |
if sys.platform == 'win32': | |
try: | |
num = int(os.environ['NUMBER_OF_PROCESSORS']) | |
except (ValueError, KeyError): | |
pass | |
elif sys.platform == 'darwin': | |
try: | |
num = int(os.popen('sysctl -n hw.ncpu').read()) | |
except ValueError: | |
pass | |
else: | |
try: | |
num = os.sysconf('SC_NPROCESSORS_ONLN') | |
except (ValueError, OSError, AttributeError): | |
pass | |
return num | |
def exec_commands(cmds): | |
''' Exec commands in parallel in multiple process | |
(as much as we have CPU) | |
''' | |
if not cmds: return # empty list | |
def done(p): | |
return p.poll() is not None | |
def success(p): | |
return p.returncode == 0 | |
def fail(): | |
sys.exit(1) | |
max_task = cpu_count() | |
processes = [] | |
while True: | |
while cmds and len(processes) < max_task: | |
task = cmds.pop() | |
logging.debug(list2cmdline(task)) | |
processes.append(Popen(task)) | |
for p in processes: | |
if done(p): | |
if success(p): | |
processes.remove(p) | |
else: | |
fail() | |
if not processes and not cmds: | |
break | |
else: | |
time.sleep(0.05) | |
commands = [ | |
['./pods.sh'], | |
['./endpoints.sh'], | |
['./dig-to-json.sh'], | |
] | |
exec_commands(commands) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment