Skip to content

Commit 427bc73

Browse files
Final Source Code Commit v1.0
Final Source Code Commit for Python Threading Tutorial
1 parent b9e4354 commit 427bc73

13 files changed

+48
-6
lines changed

_01_create_python_threads_name.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
'''
2+
Program to display the name of the running threads.
3+
4+
(c) www.xanthium.in
5+
6+
Tutorial
7+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
8+
9+
'''
10+
111
import threading
212

313

414
def do_something():
515
print('Hello from Thread')
6-
print(threading.current_thread().name)
16+
print(threading.current_thread().name) #name of the thread
717
print('\n')
818

919
def do_something_else():

_01_create_python_threads_no_join.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
(c) www.xanthium.in
55
6+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
7+
68
'''
79
import time
810
import threading #required for threading

_02_create_python_threads.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
44
(c) www.xanthium.in
55
6+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
7+
68
'''
79
import time
810
import threading #required for threading
911

1012
def do_something():
1113
print(f'\nEntered do_something() ')
14+
1215
time.sleep(2)
16+
1317
print('\nDone Sleeping in Thread\n')
1418

1519

1620
print(f'\nStart of Main Thread ')
1721

1822
t1 = threading.Thread(target = do_something) #create the thread t1
19-
t1.start() #start the threads
20-
t1.join()
23+
t1.start()
24+
t1.join() #start the threads
25+
2126

2227
print('\nEnd of Main Thread\n+---------------------------+')

_03_passing_args_python_threads.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
(c) www.xanthium.in
55
6+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
7+
68
'''
79
import time
810
import threading #required for threading

_04_return_values_from_python_thread_global_var.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
44
(c) www.xanthium.in
55
6+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
7+
68
'''
79
import time
810
import threading #required for threading

_05_return_values_from_python_thread_List.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'''
22
Returning Values from Thread functions using a List direct access by the thread
33
4+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
5+
46
(c) www.xanthium.in
57
68
'''

_06_return_values_from_python_thread_List_args.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
Returning Values from Thread functions using a List
33
using an argument on threading.Thread(target,args = (global_list,) )
44
5+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
6+
57
(c) www.xanthium.in
68
79
'''

_07_locks_python-threads.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
88
(c) www.xanthium.in
99
10+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
11+
1012
'''
1113

1214
import time
@@ -27,15 +29,16 @@ def update_list_A(var_list): #function to write A's to the List
2729

2830

2931
def update_list_B(var_list): #function to write B's to the List
32+
3033
print('update_list_B thread called ')
3134

32-
#lock.acquire()
35+
lock.acquire()
3336

3437
for _ in range(10):
3538
var_list.append('B')
3639
time.sleep(0.10)
3740

38-
#lock.release()
41+
lock.release()
3942

4043
lock = threading.Lock()
4144

_08_locks_context_manager_python-threads.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
88
(c) www.xanthium.in
99
10+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
11+
1012
'''
1113

1214
import time

_09_events_python-threads.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
event.wait()
66
77
(c) www.xanthium.in
8+
9+
https://www.xanthium.in/creating-threads-sharing-synchronizing-data-using-queue-lock-semaphore-python
810
'''
911

1012
import time
@@ -14,7 +16,7 @@ def function_t():
1416

1517
print('Entered t1 thread func')
1618

17-
my_event.wait(timeout = 10) # wait for the event to be set in main thread
19+
my_event.wait(timeout =10) # wait for the event to be set in main thread
1820

1921
print('Event is set,so this line gets printed')
2022

0 commit comments

Comments
 (0)