- Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlesson03_enumerate_threads.py
More file actions
Latest commit
25 lines (23 loc) · 719 Bytes
/
Copy pathlesson03_enumerate_threads.py
File metadata and controls
25 lines (23 loc) · 719 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# SuperFastPython.com
# example of getting a list of active threads
fromtimeimportsleep
fromthreadingimportThread
importthreading
# custom function to be executed in a new thread
deftask():
# block for a moment
sleep(1)
# protect the entry point
if__name__=='__main__':
# create a number of new threads
threads= [Thread(target=task) for_inrange(5)]
# start the new threads
forthreadinthreads:
thread.start()
# get a list of all running threads
running_threads=threading.enumerate()
# report a count of active threads
print(f'Active Threads: {len(running_threads)}')
# report each in turn
forthreadinrunning_threads:
print(thread)