I need help understanding a concept in Python.
I want to print the specific value of a variable running inside a python loop at the point in time that the import/print code is actually run.
By 'current' value, I mean the (single) value of the variable at the point in time the import and print code is actually run.
The actual code/reason I need to do this is a little abstract and would likely complicate things if I use the exact code, so I have provided a simplified example below for the sake of simplicity and clarity.
I have two files:
file1.py
import datetime
while True:
x = datetime.datetime.now()
print x
This when run prints and scrolls the current date/time as shown below:
2017-03-08 11:34:22.363000
2017-03-08 11:34:22.363000
2017-03-08 11:34:22.452000
2017-03-08 11:34:22.452000
2017-03-08 11:34:22.782000
2017-03-08 11:34:22.782000
2017-03-08 11:34:22.970000
....
I would like to be able to run another python file/process that imports and prints the specific value of x at the specific time the import and print code is actually run.
As an example, if the import/print code from file2.py is executed at 2017-03-08 11:34:22.782000 just the following should be printed...
2017-03-08 11:34:22.782000
Process finished with exit code 1
The issue that I have is that when I run the below import and print statements, the date/time prints in a scrolling list which just seems to mirror what file1.py is doing and does not return the specific point in time value of x as I would expect...
file2.py
import file1
print file1.x
So in short, my question is, how can I import and print a specific point in time value of a variable from a repeating process rather than importing and printing the full repeating process.
I hope this makes sense and forgive me if I am missing something here but I have been trying to find a solution to this without success so thought I would see if I can get any pointers here...
Any help is much appreciated. Thanks...