1

If I have a class that got threading.Thread I run the new thread with .start()

class hello(threading.Thread):
    def run():
        print "hi"
        print "bye"

So this is a thread but when I want 2 threaded functions inside a single class? How do I do that?

Because when you use .start() It uses the run function in a new thread.

1 Answer 1

4

Use the target attribute to the Thread constructor instead:

class twothreads:
    def t1(self):
        print "Hi"

    def t2(self):
        print "Bye"

t = twothreads()
threading.Thread(target=t.t1).start()
threading.Thread(target=t.t2).start()
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, didn't know it could be used that way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.