Skip to content

Instantly share code, notes, and snippets.

@skv-headless
Created February 8, 2013 07:31
Show Gist options
  • Save skv-headless/4737289 to your computer and use it in GitHub Desktop.
Save skv-headless/4737289 to your computer and use it in GitHub Desktop.
paralell
#! /bin/sh
import sys, os, time
from subprocess import Popen, list2cmdline
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()
print 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)
one_order = False
try:
if sys.argv[1]:
one_order = True
except IndexError:
one_order = False
commands = []
for i in range(20):
if one_order:
commands.append(['python','/home/skv/PycharmProjects/market_test/run.py', str(i), 'one'])
else:
commands.append(['python','/home/skv/PycharmProjects/market_test/run.py', str(i)])
exec_commands(commands)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment