Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0f52df0

Browse files
committed
添加多线程使用模板
1 parent 1c3062e commit 0f52df0

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎multi-threads.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
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()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /