Inheritance threading.Thread class does not work
Asked Answered
T

2

7

I'm new in multithreading, so the answer is probably very simple.

I'm trying to make two instances of one class and run them parallel. I've read that I can use class inheritance to do that.

class hello(threading.Thread):
    def __init__(self,min,max):
        threading.Thread.__init__(self)
        time.sleep(max)

        for i in range(1000):
            print random.choice(range(min,max))

h = hello(3,5)
k = hello(0,3)

I've noticed that this does not work (the first outputs are numbers between 3 and 5)

Could you explain what am I doing wrong?
Is this inheritance dedicated to do something else?

EDIT: I want to run these two objects parallel so since the second object has smaller wait, it has to print those numbers sooner.

According to porglezomps comment, I've tried to change the code - add a method which prints those numbers but it prints it sequentially. The problem is still there.

Thoma answered 3/9, 2015 at 18:17 Comment(3)
I can't see in what way you think you're using class inheritance here (other than in the trivial sense that the class inherits from Thread). However, you didn't mention what the problem is; how does your output differ from what you expect?Bolus
You're doing all your work in the constructor, but the constructor runs on the main thread.Lanceted
@Lanceted Ok, I've tried to make a class method which prints it but the problem is still there. class hello(threading.Thread): def __init__(self,min,max): threading.Thread.__init__(self) self.min = min self.max = max def print_it(self): time.sleep(self.max) for i in range(20): print random.choice(range(self.min,self.max))Thoma
L
25

The documentation for threading says that you should override the run() method, and then use the start() method to begin execution on a new thread. In your case, your code should be:

class Hello(threading.Thread):
    def __init__(self, min, max):
        self.min, self.max = min, max
        threading.Thread.__init__(self)

    def run(self):
        time.sleep(self.max)

        for i in range(1000):
            print random.choice(range(self.min, self.max))

# This creates the thread objects, but they don't do anything yet
h = Hello(3,5)
k = Hello(0,3)

# This causes each thread to do its work
h.start()
k.start()
Lanceted answered 3/9, 2015 at 18:33 Comment(2)
according to python thread document, you must invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.Trap
Here's the link to the statement referenced by @KenBlock.Flunky
T
2

Python's regular implementation of a thread already knows how to run a task, so unless you're creating a special kind of thread (not a special kind of task) - what you probably want is to use the regular thread:

def task(_min, _max): # 'min' and 'max' are actual functions!
    time.sleep(_max)
    for _ in range(1000):
        print random.choice(range(_min,_max))

And now create a thread to run the task:

t1 = threading.Thread(target=task, args=(3, 5,))
t2 = threading.Thread(target=task, args=(3, 5,))

t1.start()
t2.start()
Tropho answered 3/9, 2015 at 18:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.