开发者

" RuntimeError: thread.__init__() not called" when subclassing threading.Thread

I need to run as many threads of class Observer as there are elements in list dirlist. When I run it python console it works all right.

class Observer(Thread):
    def run(self):
        naptime = random.randint(1,10)
        print(self.name + ' starting, running for %ss.' % naptime)
        time.sleep(naptime)
        print(self.name + ' done')

observers = {}
for d in dirlist:
    observers[d] = Observer()
    observers[d].start()

But when I try to do it from a Master thread which is supposed to spawn the Observer threads I get errors.

class Master(Thread):
    d开发者_开发问答ef __init__(self, dirlist):
        self.dirlist = dirlist
    def run(self):
        observers = {}
        for d in dirlist:
            observers[d] = Observer()
            observers[d].start()
        while True:
            time.sleep(3600)

master_thread = Master(dirlist)
master_thread.start()

The call to Master.start results in:

RuntimeError: thread.__init__() not called

This looks strange to me.

I am unable to understand whats the difference between both cases.

Can anybody figure out a solution to my problem ?

Somehow following doesn't produce an error, and I don't understand why.

class Master(Thread):
    def set(self, dirlist):
        self.dirlist = dirlist
    def run(self):
        observers = {}
        for d in dirlist:
            observers[d] = Observer()
            observers[d].start()
        while True:
            time.sleep(3600)

master_thread = Master()
master_thread.set(dirlist)
master_thread.start()


>>> master_thread.start()
RuntimeError: thread.__init__() not called

Make sure to call Thread.__init__() in your Master.__init__:

class Master(Thread):
    def __init__(self, dirlist):
        super(Master, self).__init__()
        self.dirlist = dirlist


Well i know it's late to answer but, what the hell, i am a newbie in python but this same thing was happening to me, so i went back to read the python tutorial and it has some differences with what we both we're trying, hope it helps. instead of this

import threading

class Master(Thread):
    def set(self, dirlist):
        self.dirlist = dirlist
    def run(self):
        observers = {}
        for d in dirlist:
            ...

class according to the python tutorial:

class Master(threading.Thread):

this line is missing:

threading.Thread.__init__(self)

so it will end up being:

import threading

class Master(threading.Thread):
    def __init__(self, dirlist):
        threading.Thread.__init__(self)
        self.dirlist = dirlist
    def run(self):
        observers = {}
        for d in dirlist:
            ...

and that should work, at least work for me. I hope it was helpfull.

And your second try using the set method works, because you are not overriding the

__init__ method

from Thread therefore the original init method from the parent class is used it runs as it's supposed.


Error is clear, you should call thread.__init__():

def __init__(self, dirlist):
    super(Master, self).__init__()
    self.dirlist = dirlist       
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜