首页 > 代码库 > python线程Example

python线程Example

 1 # -*- coding:utf-8 -*-
 2 import time
 3 import pymongo
 4 from threading import Thread
 5 from Queue import Queue
 6 
 7 start  = time.time()
 8 
 9 f = open("ids.txt")
10 q = Queue()
11 
12 def work():
13     connection=pymongo.Connection(127.0.0.1,27017)
14     db = connection.pps_legual_cache
15     c = db.episode1
16     while 1:
17         line = q.get()
18         print c.find_one(int(line))
19         q.task_done()
20 
21 for i in range(5):
22     t = Thread(target=work)
23     t.setDaemon(True)
24     t.start()
25 
26 
27 for line in f.readlines():
28     q.put(int(line))
29 
30 q.join()
31 
32 end = time.time()
33 
34 print end-start