1

A quick question for Python 2.7

Are global variables visible to a subprocess?

Can a subprocess change the values of global variables?

Many thanks.

Shirley
  • 69
  • 1
  • 8
  • if you want to run closely related Python code in child processes then consider [`multiprocessing` module](http://docs.python.org/3/library/multiprocessing.html): it allows to send objects between processes just by passing them as function parameters (among other IPC methods) – jfs Mar 27 '14 at 17:51

3 Answers3

3

No, global variables are not visible to a sub-process. Variables are private to each process. If you want to share variables then you need to use some form of inter-process communication.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

The processes doesn't share the variables in a general operating system terms. Use some communication mechanism like message passing, shared memory etc to achieve the inter process communication.

Ankit Kumar
  • 1,433
  • 1
  • 16
  • 24
  • Can you share an example of inter-process communication? – Shirley Mar 27 '14 at 14:27
  • you can refer to this discussion for this [http://stackoverflow.com/questions/6920858/interprocess-communication-in-python](http://stackoverflow.com/questions/6920858/interprocess-communication-in-python) – Ankit Kumar Mar 27 '14 at 14:31
0

Maybe the simplest way is write those into a file and read from the file in another process, although this might take extra time.

Shirley
  • 69
  • 1
  • 8