-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.py
More file actions
39 lines (26 loc) · 727 Bytes
/
thread.py
File metadata and controls
39 lines (26 loc) · 727 Bytes
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
import _thread
from time import ctime, sleep
loops = [4, 2]
def loop(nloop, nsec, lock):
print('start loop', nloop, 'at:', ctime())
sleep(nsec)
print('loop', nloop, 'done at:', ctime())
lock.release()
def main():
print('starting at:', ctime())
locks = []
nloops = range(len(loops))
for _ in nloops:
lock = _thread.allocate_lock()
lock.acquire()
locks.append(lock)
print(locks)
for i in nloops:
_thread.start_new_thread(loop, (i, loops[i], locks[i]))
# 一直等待,直到不满足内层while
for i in nloops:
while locks[i].locked():
pass
print('All Done at:', ctime())
if __name__ == '__main__':
main()