There are actually two questions.
- How to run 5 process in parallel.
- How to run 5 threads in parellel.
There are actually two questions.
How to run 5 process in parallel
Use multiprocessing package for this
from multiprocessing import Pool
def f(x):
return x*x
if __name__ == '__main__':
p = Pool(5)
print(p.map(f, [1, 2, 3]))
How to run 5 threads in parellel.
Use threading package for this
import threading
def f(x):
print(x*x)
if __name__ == "__main__":
threads=[]
for c in range(1,6):
t = threading.Thread(target=f,args=(c,))
threads.append(t)
t.start()
x=input("Press any key to exit")
You can run 5 python processes in parallel with:
script.py &
script.py &
script.py &
script.py &
script.py &
While that is extremely simple and effective, it could get tedious with larger degrees of parallelism, so you could use GNU Parallel:
parallel -j5 script.py ::: {1..5}