Created
October 27, 2019 17:22
-
-
Save eenchev/6944599c2de7932c0642c146e4c06e38 to your computer and use it in GitHub Desktop.
Demonstration of Python threading library, specifically threading.Event class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import threading | |
class FooBar: | |
def __init__(self, n): | |
self.n = n | |
self.fooCanRun = threading.Event() | |
self.barCanRun = threading.Event() | |
self.fooCanRun.set() | |
def foo(self, printFoo: 'Callable[[], None]') -> None: | |
for i in range(self.n): | |
if not self.fooCanRun.isSet(): | |
self.fooCanRun.wait() | |
# printFoo() outputs "foo". Do not change or remove this line. | |
printFoo() | |
self.fooCanRun.clear() | |
self.barCanRun.set() | |
def bar(self, printBar: 'Callable[[], None]') -> None: | |
for i in range(self.n): | |
if not self.barCanRun.isSet(): | |
self.barCanRun.wait() | |
# printBar() outputs "bar". Do not change or remove this line. | |
printBar() | |
self.barCanRun.clear() | |
self.fooCanRun.set() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment