Skip to content

threaded_furkan_baytak.py #790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions Week07/threaded_furkan_baytak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import threading

def threaded(n):
"""
Decorator to create n threads for a function.

Args:
n (int): Number of threads to create.

Returns:
function: A decorated function that runs in n threads.
"""
def decorator(func):
def wrapper(*args, **kwargs):
threads = []

# Function to be run in threads
def target_func():
func(*args, **kwargs)

# Create and start threads
for _ in range(n):
thread = threading.Thread(target=target_func)
threads.append(thread)
thread.start()

# Wait for all threads to complete
for thread in threads:
thread.join()

return wrapper
return decorator
Loading