How can I run a program hundreds of times using Python?

I have a program which I need to call more than 100 times with different values.

I want to do it simultaneously that is not wait for one call to complete and then to start again.

How can I achieve it ?

Is multithreading the solution to it.

I am using python for it.

Answers


You can do this with the subprocess module. Here is an example that runs the sleep command a hundred times, with a sleep value between 0 and 10 seconds. It runs them all in parallel and then exits when they all finish.

import subprocess
import time

bin_path = 'sleep'
invocation_args = [[str(x*0.1)] for x in range(0,100)]

subprocs = []
for args in invocation_args:
    subprocs.append(subprocess.Popen([bin_path] + args))

while len(subprocs) > 0:
    subprocs = [p for p in subprocs if p.poll() is None]
    time.sleep(0.05)

print 'Finished running all subprocs'

Need Your Help

Image resizer with 8 handles and drag & drop

javascript jquery image drag-and-drop resize

I'm currently building a simple app that overlaps one image on top of another. The top one needs to be draggable and resizable. Initially I thought jQuery UI would be a good idea, there are two main

Solving an integer linear program: why are solvers claiming a solvable instance is infeasible?

cplex lpsolve gurobi integer-programming scip

I'm trying to solve integer programming problems. I've tried both use SCIP and LPSolve

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.