Home > Software engineering >  Is there a reason for this difference between Python threads and processes?
Is there a reason for this difference between Python threads and processes?

Time:04-09

When a list object is passed to a python (3.9) Process and Thread, the additions to the list object done in the thread are seen in the parent but not the additions done in the process. E. g.,

from multiprocessing import Process
from threading import Thread


def job(x, out):
    out.append(f'f({x})')


out = []
pr = Process(target=job, args=('process', out))
th = Thread(target=job, args=('thread', out))
pr.start(), th.start()
pr.join(), th.join()
print(out)

This prints ['f(thread)']. I expected it to be (disregard the order) ['f(thread)', 'f(process)'].

Could someone explain the reason for this?

CodePudding user response:

There's nothing Python-specific about it; that's just how processes work.

Specifically, all threads running within a given process share the process's memory-space -- so e.g. if thread A changes the state of a variable, thread B will "see" that change.

Processes, OTOH, each get their own private memory space that is inaccessible to all other processes. That's done deliberately as a way to prevent process A from accidentally (or deliberately) reading or corrupting the memory of process B.

When you spawn a child process, the new child process gets its own memory-space that initially contains the a copy of all the data in the parent's memory space, but it is a separate space, so changes made by the child will not be visible to the parent (and vice-versa).

  • Related