The two options suggested in comments are probably your best bets. Here are small working examples:
Here, b.py
sets status
with the current time, and prints it.
b.py
import time
status = time.time()
print status
a.py (subprocess)
import subprocess
p = subprocess.Popen('python -u b.py', stdout=subprocess.PIPE)
(out,_) = p.communicate()
b_status = out
print b_status
Output:
1427685638.46
(Note, this extra space here is intended, b_status
will have a trailing newline. This is something to keep in mind.)
a.py (import)
import b
b_status = b.status # Directly access b.py's status variable
print b_status
Output:
1427685801.45
1427685801.45
Note that status
is displayed twice using the second method. Once in b.py's print status
and once in a.py's print b_status
. Don't let it confuse you, b_status
contains only the timestamp, and no trailing newline like the first approach.
(In fact, it's "displayed" twice in the first method as well, but the output from b.py is captured by a.py, which is how b_status
is set when using that approach.)
In general, I think importing is a better alternative, since printing and parsing it is much more error prone.