Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions threading_with_condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Prints numbers from 0 to 99 sequentially from two threads with condition"""

import threading


class ReporterThread(threading.Thread):
def __init__(self, first_val, condition):
threading.Thread.__init__(self)
self.__data = range(first_val, 100, 2)
self.name = 'Thread-{}'.format(first_val)
self.condition = condition

def run(self):
for print_item in self.__data:
with self.condition:
self.condition.wait()
print('{name}: {item}'.format(name=self.name, item=print_item))
self.condition.notify()


if __name__ == "__main__":
condition = threading.Condition()

[ReporterThread(start_val, condition).start() for start_val in range(2)]

with condition:
condition.notify()
31 changes: 31 additions & 0 deletions threading_with_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Prints numbers from 0 to 99 sequentially from two threads with event"""
import threading
import time


class PrintThread(threading.Thread):
def __init__(self, first_val, print_event):
super().__init__()
self.name = 'Thread-{}'.format(first_val)
self.__data = range(first_val, 101, 2)
self.event = print_event

def run(self):
for print_item in self.__data:
self.event.wait()
self.event.clear()
print('{name}: {item}'.format(name=self.name, item=print_item))
self.event.set()
time.sleep(0.5)


if __name__ == "__main__":
my_event = threading.Event()
my_event.set()

thread1 = PrintThread(0, my_event)
thread2 = PrintThread(1, my_event)

thread1.start()
time.sleep(0.2)
thread2.start()
25 changes: 25 additions & 0 deletions threading_with_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Prints numbers from 0 to 99 sequentially from two threads with lock"""

import threading
import time


class ReporterThread(threading.Thread):
print_lock = threading.Lock()

def __init__(self, first_val):
threading.Thread.__init__(self)
self.__data = range(first_val, 100, 2)
self.name = 'Thread-{}'.format(first_val)

def run(self):
for print_item in self.__data:
with self.print_lock:
print('{name}: {item}'.format(name=self.name, item=print_item))
time.sleep(0.5)


if __name__ == "__main__":
for item in range(2):
ReporterThread(item).start()
time.sleep(0.2)
25 changes: 25 additions & 0 deletions threading_with_semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Prints numbers from 0 to 99 sequentially from two threads with semaphore"""

import threading
import time


class ReporterThread(threading.Thread):
semaphore = threading.Semaphore()

def __init__(self, first_val):
threading.Thread.__init__(self)
self.__data = range(first_val, 100, 2)
self.name = 'Thread-{}'.format(first_val)

def run(self):
for print_item in self.__data:
with self.semaphore:
print('{name}: {item}'.format(name=self.name, item=print_item))
time.sleep(0.5)


if __name__ == "__main__":
for item in range(2):
time.sleep(0.2)
ReporterThread(item).start()
20 changes: 20 additions & 0 deletions threading_with_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Prints numbers from 0 to 99 sequentially from two threads with timer"""

import threading
import time


def print_seq(first_val):
"""Prints numbers from first_val up to 100 with step 2"""

for print_item in range(first_val, 101, 2):
print('{item}'.format(item=print_item))
time.sleep(0.2)


if __name__ == "__main__":
thread1 = threading.Timer(interval=0.0, function=print_seq, args=(0,))
thread1.start()

thread2 = threading.Timer(interval=0.1, function=print_seq, args=(1,))
thread2.start()