0

Apart from the memory footprint which is more in case of multiprocessing is there any motivation to use multithreading? From what I know the GIL in cpython would allow only a single thread to be executed whereas when you create an entirely new process, it effectively bypasses the GIL and lets the OS schedule it. Are there any scenarios where multithreading seems a better option than multiprocessing?

Rinkesh P
  • 588
  • 4
  • 13
  • > In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing or concurrent.futures.ProcessPoolExecutor. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously. – Alexander May 14 '22 at 13:16
  • 1
    WIth multithreading, all the threads to share the same memory-space, which makes it much easier and more efficient to share data structures between threads. With multiple processes, any data generated within one process would need to be serialized, transmitted via some IPC mechanism, and deserialized by the other processes, before the other processes could access it. – Jeremy Friesner May 14 '22 at 13:23
  • @JeremyFriesner agreed. But will the penalty of IPC and other processing be slower than that of multithreading? I guess even with those overheads multiprocessing would still outperform multithreading. – Rinkesh P May 14 '22 at 13:42
  • @RinkeshP that would depend a lot on how large the shared data structures are, and how often they need to be updated/retransmitted. – Jeremy Friesner May 14 '22 at 13:44
  • Re, "the GIL in cpython would allow only a single thread to be executed." That's not entirely true. The GIL prevents any two threads from simultaneously using CPU time, but in many programs, we may be happy enough if one thread is allowed to use CPU time while other threads _wait_ for things (e.g., wait for I/O, for network packets, for time intervals.) One classic example is a network server, in which threads spend most of their time waiting for messages from various clients, and they spend relatively little time acting on those messages and sending replies. – Solomon Slow May 14 '22 at 14:07

0 Answers0