Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 how to do setattr() from an imported module
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 file to the file in order to clean up the file and use the 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 , nor do i know if that is even a good idea.

"" 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")) )

# moved this function to
#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
   while icnt < 5 :
     icnt =icnt + 1
     print("i am the main loop talking")
   some_thread.some_thread_en= False
"" contents:

import threading

def thread_mod_func(mod_val):
  setattr(some_thread, "some_thread_val" , mod_val)
looks like some_thread should be replaced with a real attribute name.
I think it is easier to subclass Thread:
from subfile import OurThread
from time import sleep

if __name__ == '__main__':
    some_thread = OurThread()
    for icnt in range(5):
        print('This is the main loop talking')
    some_thread.en = False
Here is subfile
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))

    def mod_func(self, value):
        self.val = value
and the output
λ python3 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
Thanks Gribouillis that example looks pretty good. i need to to sit down and learn more about OOP in python.

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  argparse and imported modules spatialdawn 2 1,570 Dec-01-2018, 12:35 PM
Last Post: spatialdawn
  global namespace of an imported function (2 Qs) Skaperen 4 810 Oct-09-2018, 12:30 AM
Last Post: Skaperen
  passing a value to imported code Skaperen 0 514 Sep-28-2018, 03:59 AM
Last Post: Skaperen
  function NOT imported from a module Skaperen 10 1,492 Aug-31-2018, 07:30 AM
Last Post: Gribouillis
  [SYS + OS] Script not Imported, Error Found ! JamieVanCadsand 2 1,381 Sep-22-2017, 08:03 PM
Last Post: snippsat
  decorate an imported function? Skaperen 3 3,554 May-22-2017, 08:05 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)