Skip to content

Instantly share code, notes, and snippets.

@thiagolcks
Last active August 31, 2016 22:55
Show Gist options
  • Save thiagolcks/17bd70d0170b923984005da10afa7f7f to your computer and use it in GitHub Desktop.
Save thiagolcks/17bd70d0170b923984005da10afa7f7f to your computer and use it in GitHub Desktop.
Playing with threads using Python
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