Created
April 10, 2014 01:21
-
-
Save torufurukawa/10335350 to your computer and use it in GitHub Desktop.
threading
This file contains hidden or 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
from time import sleep | |
from threading import Event, Thread | |
def main(): | |
# set | |
to_run = Event() | |
to_run.set() | |
# start thread | |
worker = Worker(to_run, 1) | |
worker.start() | |
print 'worker started' | |
# wait for 5 sec | |
sleep(5) | |
# clear | |
to_run.clear() | |
class Worker(Thread): | |
def __init__(self, to_run, interval): | |
Thread.__init__(self) | |
self.to_run = to_run | |
self.interval = interval | |
def run(self): | |
while True: | |
if not self.to_run.is_set(): | |
break | |
print 'hello' | |
sleep(self.interval) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment