-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread.py
More file actions
47 lines (40 loc) · 1.05 KB
/
Thread.py
File metadata and controls
47 lines (40 loc) · 1.05 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
#!/usr/bin/python
# by tianming 2013-06-26
import threading
import time
class AutoLock:
def __init__(self, lock):
self._lock = lock
self._lock.acquire()
#print "AutoLock __init__"
def __del__(self):
self._lock.release()
#print "AutoLock __del__"
#threading is good enought, do not package any more,just give an example
g_int = 0
g_lock = threading.RLock()
class MyTestThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
global g_int
while g_int < 10:
auto_lock = AutoLock(g_lock)
print "ouput %d for thread %d" %(g_int, threading.current_thread().ident)
g_int += 1
print "ready to sleep for %d" %threading.current_thread().ident
time.sleep(1)
print "end to sleep for %d" %threading.current_thread().ident
#for debug and example
if __name__ == "__main__":
thread_set = set()
thread_num = 10
while thread_num > 0:
thread_handle = MyTestThread()
thread_handle.start()
thread_set.add(thread_handle)
thread_num -= 1
for i in thread_set:
i.join()
else:
pass