File tree 2 files changed +37
-126
lines changed 2 files changed +37
-126
lines changed Original file line number Diff line number Diff line change
1
+ # -*- coding:utf-8 -*-
2
+ import threading
3
+ import Queue
4
+ '''
5
+ 多线程使用模板
6
+ '''
7
+ SHARE_Q = Queue .Queue () #构造一个不限制大小的的队列
8
+ _WORKER_THREAD_NUM = 3 #设置线程个数
9
+
10
+ class MyThread (threading .Thread ) :
11
+ def __init__ (self , func ) :
12
+ super (MyThread , self ).__init__ ()
13
+ self .func = func
14
+ def run (self ) :
15
+ self .func ()
16
+
17
+ def worker () :
18
+ global SHARE_Q
19
+ while not SHARE_Q .empty ():
20
+ item = SHARE_Q .get () #获得任务
21
+ print "Processing : %s\n " % item
22
+ # time.sleep(1)
23
+ def main () :
24
+ global SHARE_Q
25
+ threads = []
26
+ for task in xrange (1000 ) : #向队列中放入任务
27
+ SHARE_Q .put (task )
28
+
29
+ for i in xrange (_WORKER_THREAD_NUM ) :
30
+ thread = MyThread (worker )
31
+ thread .start ()
32
+ threads .append (thread )
33
+
34
+ for thread in threads :
35
+ thread .join ()
36
+ if __name__ == '__main__' :
37
+ main ()
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments