I have a python script where I have created a thread. Below is the code snippet
stop_thread = False
get_data = False
status = ""
subject_id = ""
def get_data_function():
global stop_thread
global get_data
global status
global subject_id
while stop_thread is False:
if get_data:
# SOME CODE
# SOME MORE CODE
(status, subject_id) = tive.extract_data()
get_data = False
time.sleep(1)
def main():
global stop_thread
global get_data
global status
global subject_id
thread = Thread(target=get_data_function)
thread.start()
res_dict = dict()
while True:
# SOME CODE
# SOME MORE CODE
if some_condition:
get_data = True
res_dict['status'] = status
res_dict['subject_id'] = subject_id
# SOME CODE
# SOME MORE CODE
In above code I have defined a thread and its function get_data_function(). This function calls tive.extract_data() which gives status, subject_id. I have defined these variables as global so that once we have the value of these variables , I can use it in main function.
In main function, after some_condition is True we need to get the values of status and subject_id so I set global variable get_data as True which enables the get_data_function main code and returns the data but the problem is that tive.extract_data() takes 2-3 secs to respond due to which res_dict['status'] = status and res_dict['subject_id'] = subject_id in main function gives error for that 2-3 secs and after that it starts working fine.
Is there any other way of handling the values of these variables in optimized way so that until we don't have values for these variables, we don't get errors. Please help. Thanks