-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmulti_processing.py
More file actions
56 lines (44 loc) · 1.33 KB
/
multi_processing.py
File metadata and controls
56 lines (44 loc) · 1.33 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
# threading
import threading
import time
def worker(num):
time.sleep(2)
print("The num is %d" % num)
return 0
def multi_threading():
for i in range(10):
t = threading.Thread(target=worker, args=(i,), name='t.%d' % i)
t.start()
def countdown(n):
while n>0:
print("T-minus", n)
n -= 1
time.sleep(1)
# http://python3-cookbook-personal.readthedocs.io/zh_CN/latest/c12/p01_start_stop_thread.html?highlight=daemon
# http://funhacks.net/explore-python/Process-Thread-Coroutine/coroutine.html
# https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001407503089986d175822da68d4d6685fbe849a0e0ca35000
class CountdownTask():
def __init__(self):
self._running = True
def terminate(self):
self._running = False
def run(self, n):
while self._running and n > 0:
print("T-minus", n)
n -= 1
time.sleep(2)
if __name__ == '__main__':
# multi_threading()
# t = threading.Thread(target=countdown, args=(10,), daemon=True)
# t.start()
# t.join()
# if t.is_alive():
# print('Still running')
# else:
# print('Completed')
c = CountdownTask()
t = threading.Thread(target=c.run, args=(10,))
t.start()
# c.terminate()
t.join()