Last active
March 3, 2018 00:20
-
-
Save cmccormack/e051592edf291adb6954754601f2f328 to your computer and use it in GitHub Desktop.
Testing Python3 Threading
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
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