Script to run a program with k instances (from a list) at a time

In the python code below, k = 4.

# directory with instances
pwd = '/dir/to/instances/' 
# executable (w/ path)
prog = '/dir/to/executable/./prog'

from multiprocessing import Pool
import subprocess
import os

def f(id):
    subprocess.call([prog, str(id)])

p = subprocess.Popen(['ls', pwd], stdout=subprocess.PIPE)
l = p.communicate()[0].split()
l = [x for x in l if os.path.isfile(pwd + x)]

pool = Pool(processes=4)             
pool.map(f, l)  
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s