update,
This commit is contained in:
34
hdhdjshxh/task1/test_threading.py
Normal file
34
hdhdjshxh/task1/test_threading.py
Normal file
@@ -0,0 +1,34 @@
|
||||
import threading
|
||||
|
||||
helloworld = "start"
|
||||
|
||||
def task1(name):
|
||||
print(f"Task 1 executing with name: {name}")
|
||||
return f"Hello, {name}!"
|
||||
|
||||
def task2(age):
|
||||
global helloworld
|
||||
helloworld = 'done'
|
||||
print(f"Task 2 executing with age: {age}")
|
||||
return f"You are {age} years old."
|
||||
|
||||
# Create thread objects for each task, passing arguments as a tuple
|
||||
thread1 = threading.Thread(target=task1, args=("John",))
|
||||
thread2 = threading.Thread(target=task2, args=(25,))
|
||||
|
||||
# Start the threads
|
||||
thread1.start()
|
||||
thread2.start()
|
||||
|
||||
# Wait for both threads to finish execution
|
||||
thread1.join()
|
||||
thread2.join()
|
||||
|
||||
# Retrieve the return values from each thread
|
||||
result_task1 = thread1.result if hasattr(thread1, 'result') else None
|
||||
result_task2 = thread2.result if hasattr(thread2, 'result') else None
|
||||
|
||||
print("All tasks completed")
|
||||
print("Result from Task 1:", result_task1)
|
||||
print("Result from Task 2:", result_task2)
|
||||
print(helloworld)
|
Reference in New Issue
Block a user