December 2018
Beginner to intermediate
796 pages
19h 54m
English
Another way to make threads communicate is to fire events. Let me quickly show you an example of that:
# evt.pyimport threadingdef fire(): print('Firing event...') event.set()def listen(): event.wait() print('Event has been fired')event = threading.Event()t1 = threading.Thread(target=fire)t2 = threading.Thread(target=listen)t2.start()t1.start()
Here we have two threads that run fire and listen, respectively firing and listening for an event. To fire an event, call the set method on it. The t2 thread, which is started first, is already listening to the event, and will sit there until the event is fired. The output from the previous example is the following:
$ python evt.pyFiring event...Event has been fired
Events are great ...
Read now
Unlock full access