Python Forum
how to do setattr() from an imported module - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to do setattr() from an imported module (/thread-21228.html)



how to do setattr() from an imported module - nutron - Sep-19-2019

I am trying to modify an attribute of a threaded function in the main python script from a function that has been imported from an external module.

I get this error : NameError: global name 'some_thread' is not defined

My intention is to move some functions out of the maincode.py file to the subcode.py file in order to clean up the maincode.py file and use the subcode.py file as a module. I don't know what the right way is to do setattr() from the imported module to a threaded function in the mainfile.py , nor do i know if that is even a good idea.

"mainfile.py" content :

import threading
from time import sleep
from subfile import thread_mod_func


icnt = 0

def the_thread_function(arg):
  
  while getattr(some_thread, "some_thread_en") == True:
    print("this is the value of "+ str(getattr(some_thread, "some_thread_val")) )
    sleep(0.5)

# moved this function to subfile.py
#
#def thread_mod_func(mod_val):
#  setattr(some_thread, "some_thread_val" , icnt)


if __name__ == '__main__':
   
   some_thread = threading.Thread(target=the_thread_function, args=("task",))
   setattr(some_thread, "some_thread_en" , True)                  # Initialize attribute
   setattr(some_thread, "some_thread_val" , 0)                    # Initialize attribute
   some_thread.start()
   
   
   while icnt < 5 :
     icnt =icnt + 1
     print("i am the main loop talking")
     thread_mod_func(icnt)
     sleep(1)
     
   some_thread.some_thread_en= False
   some_thread.join()
     
     
"subfile.py" contents:

import threading

def thread_mod_func(mod_val):
  setattr(some_thread, "some_thread_val" , mod_val)



RE: how to do setattr() from an imported module - Larz60+ - Sep-20-2019

looks like some_thread should be replaced with a real attribute name.


RE: how to do setattr() from an imported module - Gribouillis - Sep-20-2019

I think it is easier to subclass Thread:
# mainfile.py
from subfile import OurThread
from time import sleep

if __name__ == '__main__':
    some_thread = OurThread()
    some_thread.start()
    
    for icnt in range(5):
        print('This is the main loop talking')
        some_thread.mod_func(icnt)
        sleep(1)
        
    some_thread.en = False
    some_thread.join()
Here is subfile
# subfile.py
import threading
from time import sleep

class OurThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self) # always first. see threading doc
        self.en = True
        self.val = 0
        
    def run(self):
        while self.en:
            print("This is the value of {}".format(self.val))
            sleep(0.5)

    def mod_func(self, value):
        self.val = value
and the output
Output:
λ python3 mainfile.py This is the value of 0 This is the main loop talking This is the value of 0 This is the main loop talking This is the value of 1 This is the value of 1 This is the main loop talking This is the value of 2 This is the value of 2 This is the main loop talking This is the value of 3 This is the value of 3 This is the main loop talking This is the value of 4 This is the value of 4



RE: how to do setattr() from an imported module - nutron - Sep-20-2019

Thanks Gribouillis that example looks pretty good. i need to to sit down and learn more about OOP in python.