8000 Messing around with threads. · pythonpeixun/practice-python@a0e887e · GitHub
[go: up one dir, main page]

Skip to content

Commit a0e887e

Browse files
committed
Messing around with threads.
1 parent f6802ef commit a0e887e

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

experiments/threads.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import threading
2+
import time
3+
import random
4+
5+
6+
def execute_thread(thread_num):
7+
print("Thread {} sleeps at {}.".format(thread_num, time.strftime("%H:%M:%S", time.gmtime())))
8+
9+
sleep_time = random.randint(1, 5)
10+
11+
time.sleep(sleep_time)
12+
13+
print("Thread {} stops sleeping at {}.".format(thread_num, time.strftime("%H:%M:%S", time.gmtime())))
14+
15+
16+
def main():
17+
for i in range(10):
18+
thread = threading.Thread(target=execute_thread, args=(i, ))
19+
thread.start()
20+
21+
print("Active threads: ", threading.active_count())
22+
23+
print("thread objects: ", threading.enumerate())
24+
25+
if __name__ == '__main__':
26+
main()

0 commit comments

Comments
 (0)
0