Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a reason for this difference between Python threads and processes?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading