Skip to content

Instantly share code, notes, and snippets.

@eenchev
Created October 27, 2019 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eenchev/6944599c2de7932c0642c146e4c06e38 to your computer and use it in GitHub Desktop.
Save eenchev/6944599c2de7932c0642c146e4c06e38 to your computer and use it in GitHub Desktop.
Demonstration of Python threading library, specifically threading.Event class
#!/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