0 Star 0 Fork 10

凯沃网络科技/pydht

forked from 元谷/pydht 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sync_main.py 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
元谷 提交于 2014-02-12 23:46 . 修改存库多线程
# -*- coding: cp936 -*-
from db import DbThread
import Queue,threading,time
class SaveThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
self.dbThreads = []
for i in range(4):
dbThread = DbThread()
dbThread.setDaemon(True)
dbThread.start()
self.dbThreads.append( dbThread )
self.dbThreadNum = len(self.dbThreads)
def run(self): #Overwrite run() method, put what you want the thread do here
print "saveThread start"
queue = self.queue
threadID = 0
while True:
if queue.qsize()> 0:
hash_id = queue.get()
print "thread hash:" + hash_id
self.dbThreads[threadID].put(hash_id)
threadID = threadID+1
if threadID >= self.dbThreadNum: threadID = 0
else:
time.sleep(1)
def stop(self):
self.thread_stop = True
if __name__ == "__main__":
queue = Queue.Queue()
saveThread = SaveThread(queue)
try:
saveThread .setDaemon(True)
saveThread.start()
except:
print "Error: unable to start thread"
exit()
for hashID in open("./sync/201201.txt") :
queue.put(hashID[:-1])
while queue.qsize() > 0:
print "left:" + str(queue.qsize())
time.sleep(10)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/cskchensk/pydht.git
git@gitee.com:cskchensk/pydht.git
cskchensk
pydht
pydht
master

搜索帮助