0
votes

Is it possible to read a variable from script a.py with a independent excuted script b.py

Example: a.py

#!/usr/bin/env python3
import time

i = 0

def run():
    global i
    print(i)
    i = i+1

def get_i():
    return i

def main():
    pass        

if __name__ == "__main__":                  
    while True:
        run()        
        time.sleep(5)

print("Stopped")

b.py

#!/usr/bin/env python3

import a

print(a.get_i())

If b.py is executed it will print 0 but i want it to print the current state of i as set in a.py, so i has to be greater than 0.

I tried already with an extra c.py where i is saved as global variable an then c.py is imported in a.py and b.py but it is the same behaviour: b.py prints the initial value of i and not the value wich is set in a.py

Notice: a.py is started before b.py and is still running because of the while loop and then b.py is executed.

2
From my understanding you ARE printing i from a.py. What you're NOT doing is setting it's value in a.py. Since you are calling a.py as an import, your if is not ever being run - Rashid 'Lee' Ibrahim
Ok maybe i missed a point. a.py is started before b.py and is still running because of the while loop and then b.py is executed. - Hans Dampf
@HansDampf So, you're running two different processes simultaniously and want to share data between them? - Slam
I have a.py which is setting a variable all the time. And then I have b.py which is executed at a random time after the start of a.py and that b.py should get the variable which is set in a.py - Hans Dampf

2 Answers

0
votes

In a.py you create variable i with value of 0. This is what being imported inside b.py (btw, you can even just do import a; a.i, this will do the same).

Nothing changes state of this variable, as run in a.py in only declared, not being run. If you want this to mutate your variable, you can either run this from b.py:

from a import run, get_i
run()
print(get_i())

Or you can execute run in a.py. This is bad practice in 99%, and you should really know why you need this, if you will do so:

# a.py
i = 0

def run():
    ...

run()

This way run will be executed during import time, as python simply execute everything in file during import.

0
votes

Another way to do it is by using a common config file (e.g. config.txt) to store the i values. A minimal example of what I mean is the following:

a.py

#!/usr/bin/env python

i = 0

# create a config.txt in the same directory

def get_i():
    with open('config.txt', 'r') as f:
        last_i = f.read().splitlines()[-1]
    return last_i

def run():
    global i
    while True: 
        i += 1
        with open('config.txt', 'a') as f:
            f.write(str(i)+'\n')

if __name__ == '__main__':
    try:
        run()
    except KeyboardInterrupt:
        exit(0)

and b.py is the following:

#!/usr/bin/env python

print __import__('a').get_i()

Though this is a very simple example and what you are trying to do is a bad practice in programming. Also as I said this is a very simple example, but in real life problems accessing common resources is not that simple.