0

Can I do multithreads on each process of a multiprocess program?

For example, let say I have 4 cores available, can I add 30 threads to each of these 4 cores?

This might sound confusing so here's a sample code that shows my question better

from multiprocessing  import Process
from threading  import Thread

if __name__ == "__main__": 
    processes = []
    for i in range(4):
        processes.append(Process(target=target))
    for p in processes:
        # Can I add threads on each of these processes
        # p.append(Thread(target=target2))
        p.start()
    for p in processes:
        p.join()

This is not for a specific project it's just for my general knowledge. Thank you

Marsilinou Zaky
  • 1,038
  • 7
  • 17

1 Answers1

0

Yes, each Process can spawn in their own Thread objects. In fact, when you are using Threads without the multiprocessing module you are witnessing this since your main script is being run in its own process and it is spawning Threads! To have many processes each with their own threads will quickly become complicated to manage shared memory though (mostly because processes have separate memory), and you will have to be very careful to avoid deadlock. Your script will likely be quite lengthy to accomplish something useful with this technique. I think in general it would be best to stick to one or the other. To quote this post that you would probably be interested in:

Spawning processes is a bit slower than spawning threads. Once they are running, there is not much difference.

Reedinationer
  • 5,661
  • 1
  • 12
  • 33