InterviewHubPRO
Section Overview
Q33

Question 33 of 65

Part 3: Asynchronous Programming & Concurrency (26 - 40)

51%
Python & FastAPI•Part 3: Asynchronous Programming & Concurrency (26 - 40)

Process Pools vs Thread Pools and Shared State:

In Simple Words:

  • Thread Pool: Shares the same memory space. Fast, but subject to GIL and race conditions.
  • Process Pool: Separate memory spaces. Requires inter-process communication (IPC / Pickling) to pass data back and forth.
python
# Multiprocessing shared memory with Value or Array:
from multiprocessing import Process, Value

def increment(counter):
    with counter.get_lock(): # Synchronize access
        counter.value += 1

if __name__ == '__main__':
    shared_counter = Value('i', 0)
    p = Process(target=increment, args=(shared_counter,))
    p.start()
    p.join()
    print(shared_counter.value) # 1