I have a Thread that is checking every hour of a time and keep updating a global variable, for some reason global statement is not working and it's keeping the same value in the global variable even if the thread update it, please reference to my code:
paymentDate = "2024-03-22"
currentDate = ""
#Function that is in charge of validate the date
def isSystemValidDateReached():
global currentDate
try:
if currentDate == "":
currentDate = str(urlopen('http://just-the-time.appspot.com').read().strip().decode('utf-8'))
if paymentDate < currentDate:
return True
else:
return False
except Exception as e:
log.appendToLog("Error", "critical")
#function that is being used in a thread
def getTimeFromInternet(timeSleep=3600):
global currentDate
while True:
try:
time.sleep(timeSleep)
currentDate = str(urlopen('http://just-the-time.appspot.com').read().strip().decode('utf-8'))
print("new current date {}".format(currentDate))
except Exception as e:
log.appendToLog("Error: {}".format(e), "critical")
This is a portion of how it looks in main script and this function is being checked in a while loop every certain time:
if isSystemValidDateReached():
exit()
# get time every hour
getDateFunctionThread = multiprocessing.Process(target=getTimeFromInternet)
getDateFunctionThread.start()
The problem that I'm facing is that I have a breakpoint in getTimeFromInternet function and it updates currentDate, but when the function isSystemValidDateReached() is called, I see the same old value assigned in this portion of code:
if currentDate == "":
currentDate = str(urlopen('http://just-the-time.appspot.com').read().strip().decode('utf-8'))
What needs to be changed to have this variable getting updated every hour in my threaded function?
As chepner state, how was solved is because I was using multiprocessing instead of thread, see the code as follows: