-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
threads_expl.py
37 lines (25 loc) · 892 Bytes
/
threads_expl.py
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
26
27
28
29
30
31
32
33
34
35
36
37
# Examples of Async use network service(i.e., reading a web API), database retrieve
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import threading
import time
def countdown(n):
while n > 0:
print('Down', n)
time.sleep(1)
n -= 1
def countup(stop):
x = 0
while x < stop:
print('Up', x)
time.sleep(1)
x += 1
# Sequential execution
countdown(5)
countup(5)
# Concurrent execution
# Classic solution: use threads
threading.Thread(target=countdown, args=(5,)).start()
threading.Thread(target=countup, args=(5,)).start()
# Note on threads in Python, like C they are hardware - posix threads
# the GIL (Global Interpreter Lock) prevents threads in Python from running in parallel they can only run on a single CPU