0

I am a newbie Python programmer. I interested in parallel and concurrent programming. I understand the difference between process and thread but I confused about how multiprocessing and threading work. If my program A is -

from multiprocessing import Process
for i in range(50):
    p = Process(target=worker)
    p.start()

Does it will generate 50 processes on 1 CPU or distribute on 4 CPUs (my laptop has 4 CPUs 2 Cores) and same question but change program A to program B my program B is -

from threading import Thread
for i in range(50)
    t = Thread(target=worker)
    t.start()

In program B all threads depend on 1 process or not?

Explain this to me, please. Thank you.

jar
  • 2,646
  • 1
  • 22
  • 47
L.Cole
  • 333
  • 1
  • 7

1 Answers1

0

Multiprocessing creates new subprocess, which can be run on multiple cores simultaneously.

Threading, as its name, will create a thread. Multiple threads share the same memory space, and GIL (Global Interpreter Lock) will prevent them to concurrent write. Due to GIL, threads cannot be spawned on multiple cores.

So, A will run on 4 CPUs, while B will run on one.

Kevin
  • 16