Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I was working on Python scripts that were using Threads in a class and in a particular program, threading.Event() was used a lot. For example:

class TimerClass(threading.Thread):

    def __init__(self):

        threading.Thread.__init__(self)

        self.event = threading.Event()

    def run(self):

        while not self.event.is_set():

            print "something"

            self.event.wait(120)

In the while loop,  I don't know why the code is checking the statement if it doesn't set the self.event?

1 Answer

0 votes
by (108k points)

This is because someone else was setting it. See, you will usually start a thread in one section of your application and continue to do whatever you do:

thread = TimerClass()

thread.start()

# Your task

The thread will perform its own task, while you do your set of tasks. If you want to stop the thread, you just need to call:

thread. event.set()

And the thread will stop.

Want to be a Python expert? Join this Python Training course by Intellipaat to learn more.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Nov 26, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Sep 26, 2019 in Python by Sammy (47.6k points)

Browse Categories

...