Skip to content

Commit

Permalink
version 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Beni Valotker authored and Beni Valotker committed Feb 24, 2020
0 parents commit 44c1499
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'''
configuration file
'''
1 change: 1 addition & 0 deletions factory_pattern/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from factory_pattern.factory import FactoryClass
48 changes: 48 additions & 0 deletions factory_pattern/factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from abc import ABC, abstractmethod

'''
factory design pattern
'''
class FactoryClass:
'''
Factory Class
'''
# return the request class messages
@staticmethod
def get_class(class_name):
if class_name == "thread_number_1":
return Thread_1()
elif class_name == "thread_number_2":
return Thread_2()

assert 0, 'Could not find shape ' + class_name

'''
Interface - all the class that inherit from this interface must declare methods.
'''
class FactoryInterface(ABC):
'''
Messages Interface
'''
@abstractmethod
def run(self): pass


'''
methods:
- run: print some text
'''
class Thread_1(FactoryInterface):
def __init__(self):
pass

def run(self):
print("run thread number 1")


class Thread_2(FactoryInterface):
def __init__(self):
pass

def run(self):
print("run thread number 2")
48 changes: 48 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#/path/path/
#title :Title
#description :Description.
#update date :01/01/2020 12:10
#version :1.0
#changes :new version changes description.
#python_version :3.6
#==============================================================================

import time
import threading
import thread_handler

'''
Daemon Threads - thread shut down when the caller script is done or after all the thread is close.
- name = custom thread name
- target = function to run
- args = function args (as a tuples)
- daemon = True/Flase
'''
# declare the threads object
thread_obj_1 = threading.Thread(
name="Custom Thread 1",
target=thread_handler.thread_handler,
args=("thread_number_1", ),
daemon=True
)

thread_obj_2 = threading.Thread(
name="Custom Thread 2",
target=thread_handler.thread_handler,
args=("thread_number_2", ),
daemon=True
)

# start threads running
thread_obj_1.start()
thread_obj_2.start()


# check if the thread is alive (finish is job or error)
# in this case if one thread is dead the all thread shut down
while(True):
time.sleep(1)
if thread_obj_1.is_alive() != None:
break
if thread_obj_2.is_alive() != None:
break
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'''
package dependencies version
'''
8 changes: 8 additions & 0 deletions thread_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from factory_pattern import FactoryClass

# handle the service by thread type class
def thread_handler(thread_name):
# get and create the object
factory_obj = FactoryClass.get_class(thread_name)

factory_obj.run()

0 comments on commit 44c1499

Please sign in to comment.