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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.