diff --git a/Week07/threaded_ege_enc.py b/Week07/threaded_ege_enc.py new file mode 100644 index 00000000..1c9a5c8c --- /dev/null +++ b/Week07/threaded_ege_enc.py @@ -0,0 +1,28 @@ +import threading + +def threaded(n: int): + """ + This function is a decorator which takes an integer n as an argument + and returns a threaded function. + + :param n: Number of threads + :return: Threaded function + """ + def decorator(func): + def wrapper(*args, **kwargs): + threads = [] + for _ in range(n): + t = threading.Thread(target=func, args=args, kwargs=kwargs) + t.start() + threads.append(t) + for t in threads: + t.join() + return wrapper + return decorator + +@threaded(4) +def func(value): + print(f"Hello World: {value}") + +# Test +func("dummy")