0

I have this:

#!/usr/bin/env python

import multiprocessing

class MultiprocessingTest(object):

    def __init__(self):
        self.cmd = ''

    def for_process_A(self):
        self.cmd = "AA"
        print "%s executing and cmd is %s" % (multiprocessing.current_process().name, self.cmd)


    def for_process_B(self):
        self.cmd = "BB"
        print "%s executing and cmd is %s" % (multiprocessing.current_process().name, self.cmd)


if __name__ == '__main__':

    obj =   MultiprocessingTest()

    process_A = multiprocessing.Process(target=obj.for_process_A, name='process_A')
    process_B = multiprocessing.Process(target=obj.for_process_B, name='process_B')

    process_A.start()
    process_B.start()


    process_A.join()
    process_B.join()

Question:

Do the two processes share the variable cmd?

Do both processes have a separate class MultiprocessingTest definition and work off of that?

Independent copies of which data exists in the two processes?

I am trying to understand from a theoretical standpoint what is actually happening here. Can you please comment on that?

Test Run o/p:

$ ./commonvar.py 

process_A executing and cmd is AA

process_B executing and cmd is BB
5
  • Now that is such fantastic test to run yourself! What did it show up? What is the conculsion? Commented Feb 26, 2014 at 4:14
  • @Puciek Added test run results. Commented Feb 26, 2014 at 4:16
  • So what does that test say to you? Not much right? You may want expand it to actually test what are you trying to, well, test ;) Commented Feb 26, 2014 at 4:21
  • When you make a Process, the entire process is forked - i.e. memory is copied. That's putting it a little simplistically, but for the purposes of a mental exercise, all your objects split into two - one copy stays in the parent process, the other heads off to the new process. Commented Feb 26, 2014 at 4:25
  • @Puciek Hey Puciek can you suggest what a good way to test this would be? I'd like to draw conclusion (test result) that inspires 100% confidence. Commented Feb 26, 2014 at 5:40

2 Answers 2

1

Processes don't share data. Each process is a separate container with following resources, generally speaking:

  • Code to execute
  • Stack
  • Processor time

Processes interact with outside world through Pipes.

So to answer your questions:

  • Processes will not share cmd variable.
  • Processes will have separate copies of the class code.
  • All the program data will be independent.

Further Explanation:

Behind the scenes, fork system call is used to create a process (assuming you are using *nix). Processes are heavier compared to threads because of the overhead involved in switching the conext.

Sign up to request clarification or add additional context in comments.

Comments

0

Changes which happen inside of multiprocessing shouldn't propagate back to the calling "thread" (or any of the other multiprocessing processes). If you want that sort of "shared-memory-like" behavior, you'll need to look into using a multiprocessing.Manager.

2 Comments

I am trying to understand from a theoretical standpoint what is actually happening here. Can you please comment on that?
@abc -- Sure. Essentially, where you hit the process.start(), multiprocessing starts a new python process. It basically clones your current processes state and then executes mostly independently from the current process. When it's done doing what you asked it to, it returns the values and sends them back to the current process for whatever you want to do with them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.