1.线程基本操作
1.创建线程的方式
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
|
import threading
def f1(args):
print (args)
t = threading.Thread(target = f1,args = ( 123 ,))
t.start()
import threading
class MyThread(threading.Thread):
def __init__( self ,func,args):
self .func = func
self .args = args
super (MyThread, self ).__init__()
def run( self ):
self .func( self .args)
def f2(arg):
print (arg)
obj = MyThread(f2, 123 )
obj.start()
|
2.线程锁(同时只允许一个线程进入取值。)
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
56
57
58
59
60
61
62
63
64
65
66
67
|
线程锁分类:
1.
l.acquire()
l.release()
lock = threading.Lock()
2.
l.acquire()
l.release()
lock = threadingRLock()
例子:
当有一个数字 10 ,如果同时 10 个线程去减 1 ,
那么就会产生脏数据,输出结果都输出为 0 。
import threading
import time
NUM = 10
def func(l):
global NUM
NUM - = 1
time.sleep( 2 )
print (NUM)
lock = threading.Lock()
for i in range ( 10 ):
t = threading.Thread(target = func,args = (lock,))
t.start()
import threading
import time
NUM = 10
def func(l):
global NUM
l.acquire()
NUM - = 1
time.sleep( 2 )
print (NUM)
l.release()
lock = threading.Lock()
for i in range ( 10 ):
t = threading.Thread(target = func,args = (lock,))
t.start()
import threading
import time
NUM = 10
def func(l):
global NUM
l.acquire()
NUM - = 1
l.acquire()
time.sleep( 2 )
l.release()
print (NUM)
l.release()
lock = threading.RLock()
for i in range ( 10 ):
t = threading.Thread(target = func,args = (lock,))
t.start()
|
3.信号量以及事件
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
|
信号量(semaphore)(同时允许一定数量的线程更改数据)
import threading
import time
NUM = 10
def func(i,l):
global NUM
lock.acquire()
NUM - = 1
time.sleep( 2 )
print (NUM,i)
lock.release()
lock = threading.BoundedSemaphore( 5 )
for i in range ( 30 ):
t = threading.Thread(target = func,args = (i,lock,))
t.start()
import threading
def func(i,e):
print (i)
e.wait()
print (i + 100 )
event = threading.Event()
for i in range ( 10 ):
t = threading.Thread(target = func,args = (i,event,))
t.start()
event.clear()
inp = input ( ">>" )
if inp = = "1" :
event. set ()
|
4.条件以及定时器
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
56
57
58
59
60
61
62
63
|
条件Condition (使得线程等待,只有满足某条件时,才释放n个线程)
import threading
def run(n):
con.acquire()
con.wait()
print ( "run the thread: %s" % n)
con.release()
if __name__ = = '__main__' :
con = threading.Condition()
for i in range ( 10 ):
t = threading.Thread(target = run, args = (i,))
t.start()
while True :
inp = input ( '>>>' )
if inp = = 'q' :
break
con.acquire()
con.notify( int (inp))
con.release()
import threading
def condition():
ret = False
r = input ( ">>>>" )
if r = = 'true' :
ret = True
else :
ret = False
return ret
def func(i,con):
print (i)
con.acquire()
con.wait_for(condition)
print (i + 100 )
con.release()
c = threading.Condition()
for i in range ( 10 ):
t = threading.Thread(target = func,args = (i,c,))
t.start()
from threading import Timer
def hello():
print ( "hello world" )
t = Timer( 1 ,hello)
t.start()
|
2.队列的使用(Python中队列是线程间最常用的交换数据的形式)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
1.queue 队列
基本特点:
先进先出
方法:
put 放数据,是否阻塞,阻塞时的超时时间
get 取数据,默认阻塞,是否阻塞,阻塞时的超时时间
q.empty 检查队列是否为空,为空返回 True ,否则 False
q.full 判对是否满了
q.qsize 队列里边有几个元素;当前真实个数
q.maxsize 元素最大支持的个数
q.join 如果队列任务没有完成,还在等待。
q.taskdone 每一次取完数据的时候执行q.task_done(),告诉我执行完了,系统做一个计数。表示我取完数据了
join,task_done 阻塞进程,当队列中任务执行完毕之后,不再阻塞。
2. 其他队列:
1 、后进先出
queue.LifoQueue
2 、优先级队列 (放数据的时候加上权重值;内容包括:数据 + 权重)
queue.PriorityQueue
3 、双向队列 (两边存、两边取)
queue.deque
import queue
q = queue.Queue( 10 )
q.put( 11 ,timeout = 2 )
q.put( 22 ,block = False )
q.put( 33 )
print (q.qsize())
print (q.get(block = False ))
print (q.empty())
3
11
False
q = queue.Queue( 5 )
q.put( 33 )
q.put( 44 )
print (q.get())
print (q.task_done())
print (q.get())
print (q.task_done())
33
None
44
None
q = queue.LifoQueue()
q.put( 123 )
q.put( 456 )
print (q.get())
456
q = queue.PriorityQueue()
q.put(( 1 , 'aaa' ))
q.put(( 2 , 'bbb' ))
print (q.get())
( 1 , 'aaa' )
q = queue.deque()
q.append( 123 )
q.append( 456 )
q.appendleft( 333 )
print (q.pop())
print (q.popleft())
456
333
|
3.自定义线程池
本文转自506554897 51CTO博客,原文链接:http://blog.51cto.com/506554897/1843549,如需转载请自行联系原作者
|