Skip to content

Commit 5915f30

Browse files
committed
fix: basic threading
1 parent b1c40e1 commit 5915f30

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
def thread:
3+
- formal: ???
4+
5+
- in words: ???
6+
7+
- plain english: ???
8+
9+
- intuition: ???
10+
11+
- properties:
12+
- specification:
13+
- high level: https://docs.python.org/3.11/library/threading.html
14+
- low level: https://docs.python.org/3.11/library/_thread.html#module-_thread
15+
- constraints:
16+
- CPython: Due to the Global Interpreter Lock(GIL), only one thread can execute python code at a time.
17+
- threading module not available on WebAssembly platforms(i.e. wasm32-emscripten, wasm32-wasi).
18+
- examples: ???
19+
20+
- use cases:
21+
- multiple IO bound tasks
22+
23+
- proof: ???
24+
25+
References: ???
26+
27+
"""
28+
29+
import threading
30+
31+
32+
def square(numbers):
33+
for x in numbers:
34+
squares.append(x * x)
35+
36+
37+
def cube(numbers):
38+
for x in numbers:
39+
cubes.append(x * x * x)
40+
41+
42+
if __name__ == "__main__":
43+
numbers = [i for i in range(10)]
44+
squares = []
45+
cubes = []
46+
47+
t_1 = threading.Thread(target=square, args=[numbers])
48+
t_2 = threading.Thread(target=cube, args=[numbers])
49+
50+
t_1.start()
51+
t_2.start()
52+
53+
t_1.join()
54+
t_2.join()
55+
56+
print(squares)
57+
print(cubes)

0 commit comments

Comments
 (0)