Skip to content

Commit 2faa2ce

Browse files
committed
Merge branch 'master' of github.com:leeyis/python-snippets
2 parents 2986a2f + 7f47396 commit 2faa2ce

File tree

2 files changed

+37
-126
lines changed

2 files changed

+37
-126
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()

proxyCheck.py

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)