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
19 changes: 19 additions & 0 deletions homework10/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018 EPAM Systems

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
6 changes: 6 additions & 0 deletions homework10/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This package contains examples of threads synchronization using next primitives:
1) Events
2) Conditions
3) Timer
4) Semaphores
5) Locks
21 changes: 21 additions & 0 deletions homework10/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="sync_package",
version="1.0.0",
author="Yahor Pichkur",
author_email="Yahor_Pichkur@epam.com",
description="Threads synchronization examples",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/EgorPichkur/advanced_python",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
1 change: 1 addition & 0 deletions homework10/sync_package/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "sync_package"
34 changes: 34 additions & 0 deletions homework10/sync_package/sync_condition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Homework 3: Thread Synchronization Using Conditions"""

from threading import Condition
from threading import Thread


def print_odd(cv):
"""Function to print odd numbers"""
nums = [x for x in range(101) if x % 2 == 1]
for num in nums:
with cv:
cv.wait()
print(num)
cv.notify()


def print_even(cv):
"""Function to print even numbers"""
nums = [x for x in range(101) if x % 2 == 0]
for num in nums:
with cv:
print(num)
cv.notify()
if (num != 100):
cv.wait()


if __name__ == '__main__':
condition = Condition()
odd = Thread(name='odd', target=print_odd, args=(condition,))
even = Thread(name='even', target=print_even, args=(condition,))

odd.start()
even.start()
42 changes: 42 additions & 0 deletions homework10/sync_package/sync_event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""Homework 3: Thread Synchronization Using Events"""

from threading import Event
from threading import Thread


class EventThread(Thread):
"""My thread to work with events"""

main_event = Event()

def __init__(self, thread_name):
super().__init__()

self.thread_name = thread_name

if self.thread_name == 'even':
self.nums = [x for x in range(101) if x % 2 == 0]
else:
self.nums = [x for x in range(101) if x % 2 == 1]

def run(self):
"""Implementation of thread's activity"""

for num in self.nums:
if self.thread_name == 'even':
while self.main_event.isSet():
pass
print(num)
self.main_event.set()
else:
self.main_event.wait()
print(num)
self.main_event.clear()


if __name__ == '__main__':
even_thread = EventThread('even')
odd_thread = EventThread('odd')

odd_thread.start()
even_thread.start()
43 changes: 43 additions & 0 deletions homework10/sync_package/sync_lock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Homework 3: Thread Synchronization Using Locks"""

from threading import Lock
from threading import Thread


class LockThread(Thread):
"""My thread to work with locks"""

even_lock = Lock()
odd_lock = Lock()

def __init__(self, thread_name):
super().__init__()

self.thread_name = thread_name

if self.thread_name == 'even':
self.nums = [x for x in range(101) if x % 2 == 0]
self.odd_lock.acquire()
else:
self.nums = [x for x in range(101) if x % 2 == 1]

def run(self):
"""Implementation of thread's activity"""

for num in self.nums:
if self.thread_name == 'even':
self.even_lock.acquire()
print(num)
self.odd_lock.release()
else:
self.odd_lock.acquire()
print(num)
self.even_lock.release()


if __name__ == '__main__':
even_thread = LockThread('even')
odd_thread = LockThread('odd')

odd_thread.start()
even_thread.start()
41 changes: 41 additions & 0 deletions homework10/sync_package/sync_semaphore.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Homework 3: Thread Synchronization Using Semaphores"""

from threading import Semaphore
from threading import Thread


class SemaphoreThread(Thread):
"""My thread to work with semaphores"""

even_sem = Semaphore(1)
odd_sem = Semaphore(0)

def __init__(self, thread_name):
super().__init__()

self.thread_name = thread_name
if self.thread_name == 'even':
self.nums = [x for x in range(101) if x % 2 == 0]
else:
self.nums = [x for x in range(101) if x % 2 == 1]

def run(self):
"""Implementation of thread's activity"""

for num in self.nums:
if self.thread_name == 'even':
self.even_sem.acquire()
print(num)
self.odd_sem.release()
else:
self.odd_sem.acquire()
print(num)
self.even_sem.release()


if __name__ == '__main__':
even_thread = SemaphoreThread('even')
odd_thread = SemaphoreThread('odd')

even_thread.start()
odd_thread.start()
24 changes: 24 additions & 0 deletions homework10/sync_package/sync_timer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Homework 3: Thread Synchronization Using Timer"""

from threading import Timer
from time import sleep


def timer_thread(start_num, sleep_time):
for num in range(start_num, 101, 2):
print(num)
sleep(sleep_time)


if __name__ == '__main__':
sleep_time = 1
even_start_num = 0
odd_start_num = 1

even_thread = Timer(sleep_time / 2, timer_thread,
args=[even_start_num, sleep_time])
odd_thread = Timer(sleep_time, timer_thread,
args=[odd_start_num, sleep_time])

even_thread.start()
odd_thread.start()