Skip to content

Instantly share code, notes, and snippets.

@cmccormack
Last active March 3, 2018 00:20
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 cmccormack/e051592edf291adb6954754601f2f328 to your computer and use it in GitHub Desktop.
Save cmccormack/e051592edf291adb6954754601f2f328 to your computer and use it in GitHub Desktop.
Testing Python3 Threading
from threading import Thread, Lock
from random import randint
from time import sleep
class AsyncLetter(Thread):
def __init__(self, letter):
super().__init__()
self.timer = randint(1,5)
self.letter = letter
def getTime(self):
return self.timer
def run(self):
sleep(self.timer)
lock.acquire()
print(f'[{self.timer}] {self.letter}')
lock.release()
lock = Lock()
for letter in ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']:
AsyncLetter(letter).start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment