8000 More messing around with threads. · iangon/practice-python@6366d60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6366d60

Browse files
committed
More messing around with threads.
1 parent be4ce69 commit 6366d60

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

experiments/threads.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ def execute_thread(thread_num):
1616
def main():
1717
for i in range(10):
1818
thread = threading.Thread(target=execute_thread, args=(i, ))
19+
# thread.daemon = True
1920
thread.start()
21+
# thread.join()
2022

21-
print("Active threads: ", threading.active_count())
22-
23-
print("thread objects: ", threading.enumerate())
23+
print("Active threads: ", threading.active_count())
24+
print("thread objects: ", threading.enumerate())
2425

2526
if __name__ == '__main__':
2627
main()

experiments/threads2.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import random
2+
import threading
3+
import time
4+
5+
6+
def count_up(num):
7+
8+
global database, lock
9+
10+
lock.acquire()
11+
12+
try:
13+
time.sleep(random.randrange(3))
14+
database.append(num)
15+
finally:
16+
lock.release()
17+
18+
19+
def main():
20+
21+
global database, lock
22+
23+
lock = threading.Lock()
24+
database = []
25+
26+
threads = []
27+
for i in range(15):
28+
threads.append(threading.Thread(target=count_up, args=(i+1,)))
29+
threads[i].start()
30+
31+
for th in threads:
32+
th.join()
33+
34+
print(database)
35+
36+
37+
if __name__ == '__main__':
38+
main()

experiments/typing.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import curses
2+
3+
win = curses.initscr()
4+
key = win.getch()
5+
6+
print(str(key))

0 commit comments

Comments
 (0)
0