[docs] [issue17808] No code example for Event object in threading module

Andriy Mysyk report at bugs.python.org
Sun Apr 21 05:36:20 CEST 2013


Andriy Mysyk added the comment:

Example added to threading.rst


For example, the following code demonstrates a controlled thread termination using an event object.  The event is used to request the termination of several threads.

import threading
import time

stopevent = threading.Event()

class TestThread(threading.Thread):
    def run(self):
        """ main control loop """
        print ("Thread ", self.ident, " starts")
        count = 0
        while not stopevent.is_set():
            count += 1
            stopevent.wait(1.0)
            print ("loop ", count, "in thread ", self.ident)
        print ("Thread ", self.ident, " ends")

for i in range (2):
    testthread = TestThread()
    testthread.start()
time.sleep (3)
stopevent.set()

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17808>
_______________________________________


More information about the docs mailing list