0

i am constantly changing a variable in one module and trying to use that variable in another module.But I don't see the updated variable . For eg. I have check.py

total_time=0

I have check_1.py which increments total_time from check.py

import check
for i in range(6):
    check.total_time +=1
    sleep(4)

I have check_2.py which needs to use the incremented value

import check
for i in range(5):
    print "in check 2 , total time is ", check.total_time
    sleep(3)

I am running check_1 and check_2 at the same time . check_1 keeps on increasing the value. BUT check_2 always prints 0 whereas i am expecting it to print the updated increased value. I am not sure what i am missing here .

sunyata
  • 1
  • 3

1 Answers1

0

Do I understand you right that you want to use a variable in a common module to transmit data between two separate Python scripts?

If this is what you assume would happen, you assume wrongly. Both of those scripts can definitely access the variable. Otherwise you would get an exception.

However, these scripts operate completely different memory segments that are independent of each other despite including the same Python module. Both programs have an independent copy of check.total_time and they will never see the variable of the other program.

There are a couple of possibilities to share data between scripts. Some of them are discussed here: How to share variables across scripts in python?

Hannu
  • 11,685
  • 4
  • 35
  • 51
  • Actually ,i want to share value between two different modules in a django app. One of the module is a thread and it keeps on updating a value which needs to be accessed in another module. How do i do that? – sunyata Sep 21 '17 at 15:30