Eklablog All blogs Top blogs Tech & Science
Edit post Follow this blog Administration + Create my blog
MENU

Advertising

 

 

########################################for changing variables in thread function we need to make it global or it not work

import threading

import time

state={}

 

print(globals().get("state0"))

def thread_function_1(a):

   # as we see to change global variable in function we need to call it as global variable not local variable

   global state

   while True:

    if True:

     for el in a:

      if not globals().get(el)==None and (not (globals().get(el)==state.get(el))):

       thevar=globals().get(el)   

       print(globals().get(el),thevar)

       if not globals().get(el)==state.get(el):

       

        if  state==None:

         print("var:"+el+" is added",thevar)

        else:

         print("var:"+el+" is changed",thevar)   

       state[el]=thevar

    time.sleep(0.5)

if __name__ == "__main__": 

    x1 = threading.Thread(target=thread_function_1, args=(["thevar","understood"],)) #this is listener of variables changes using threads

    x1.start()

    thevar=2

    understood=0

    time.sleep(1)

    thevar=55

    understood=5

    time.sleep(1)

    thevar="test"

    print("donw")

 

Advertising
Back to home page
Share this post
Repost0
To be informed of the latest articles, subscribe:
Comment on this post