-2

There are actually two questions.

  1. How to run 5 process in parallel.
  2. How to run 5 threads in parellel.
RedBoots
  • 33
  • 1
  • 1
  • 4
  • 1
    Have you spent any time on google at all? Have you not found _anything_ about multithreading and multiprocessing? If you've found some code online but can't get it to work, you should post it. If not, this should be closed as too broad because SO isn't google. – Aran-Fey Sep 10 '17 at 06:36
  • [Thread](https://stackoverflow.com/questions/2846653/how-to-use-threading-in-python) , [Process](https://stackoverflow.com/questions/20548628/how-to-do-parallel-programming-in-python), But most importantly: [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) – SajidSalim Sep 10 '17 at 06:39

2 Answers2

1

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")
Max
  • 1,283
  • 9
  • 20
0

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}
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432