2

Let's imagine i have a program in Python like this one (first.py):

GLOBALVAR = "pineapple"

def changeVAR(newValue):
     global GLOBALVAR
     GLOBALVAR = newValue

while(True):
   print GLOBALVAR

And i want to chante the global variable GLOBALVAR with another python program while the first is running. For instance (second.py):

from second import changeVAR

#do something
first.changeVAR("banana")

Is it possible to do something like this? Meaning, to change a global variable of a running program with another program?

Thank you in advance!

0

3 Answers 3

5

What you describe (programs sharing the same variables) is only possible between different threads (which share the same python interpreter and data), not between different processes.

Modern operating systems do not allow a program to access the memory of another program. So you cannot just reach into the memory of another process and change something there. Allowing that would create all sorts of bugs and security problems.

It is possible to communicate with another program, using interprocess communication. This can be done with e.g. sockets, memory mapped files or writing to each other's standard in- and output. There is also support for semaphores, shared memoy and message queues on UNIX-like operating systems.

The multiprocessing module provides the Queue and Pipe objects to allow related processed to talk to each other.

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

1 Comment

If there's ways to get or set a variable from a variable string and ways to get variable strings from the python script before runtime or right at runtime, there should exist a protocol/library to communicate with the runtime script and be able to get/set these with one externally as suggested. Similar to CCP, XCP... If one does not exist, shame on the founders. I will call it PCP...
3

A workaround i am using. is to create an empty file somewhere. then the one prog. could READ from this file (say every 2 sec or so) if it is empty, then do nothing. and the other could WRITE to this file if needed. After a WRITE, the other program would READ the written data from the file, use this as the new VAR. and then clear the file, ready for a new read. I am using this on a server that reads from the file, then from another program i can send "commands" (using it to voice controll lighting) to the server, turning the lights on/off etc!

Comments

0

Yes, but this is a dynamic change, so if you statically load the module again later, the change will not persist. However, anytime you use the module after the change, it will stick around.

You could even simply call first.GLOBALVAR = 'banana' to achieve the same result.

2 Comments

Importing a module does not make a connection to another running program, which seems to be what Tallis wants to do.
I totally agree with that, just wanted to explain what happens a bit. Your answer is quite helpful.

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.