Last active
August 31, 2016 22:55
-
-
Save thiagolcks/17bd70d0170b923984005da10afa7f7f to your computer and use it in GitHub Desktop.
Playing with threads using Python
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
import threading, time, random | |
class Counter (threading.Thread): | |
num = 0 | |
def reset(self): | |
self.num = 0 | |
def run(self): | |
while True: | |
time.sleep(random.uniform(0.2, 1.5)) | |
self.math() | |
print self.num | |
class Up (Counter): | |
def math(self): | |
self.num += 1 | |
class Down (Counter): | |
def math(self): | |
self.num -= 1 | |
up = Up() | |
up.daemon = True | |
up.start() | |
down = Down() | |
down.daemon = True | |
down.start() | |
while True: | |
key = raw_input() | |
up.reset() | |
down.reset() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment