python - Why does the instance need to be recreated when restarting a thread? -


imagine following classes:

class object(threading.thread):     # initialisation blabla     def run(self):         while true:             #             sleep(1)  class checker():     def check_if_thread_is_alive(self):         o = object()         o.start()          while true:             if not o.is_alive():                 o.start() 

i want restart thread in case dead. doens't work. because threads can started once. first question. why this?

for far know have recreate each instance of object , call start() start thread again. in case of complex objects not practical. i've read current values of old object, create new 1 , set parameters in new object old values. second question: can done in smarter, easier way?

the reason why threading.thread implemented way keep correspondence between thread object , operating system's thread. in major oss threads can not restarted, may create thread with thread id.

if recreation problem, there no need inherit class threading.thread, pass target parameter thread's constructor this:

class myobj(object):     def __init__(self):         self.thread = threading.thread(target=self.run)     def run(self):         ... 

then may access thread member control thread execution, , recreate needed. no myobj recreation required.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -