We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1c3062e commit 0f52df0Copy full SHA for 0f52df0
multi-threads.py
@@ -0,0 +1,37 @@
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
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()
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments