才10k 左右的PV,每日的交互日志就已经爆增到500M 的级别,XML 真是万恶阿!
电脑技术002pc网从python字典的key以是列表Python 多线程分析来看,对python字典的key以是列表Python 多线程的结果。
为了提高日志处理速度,引入多线程。(如果保持这个趋势,我想Hadoop 的技术储备需要尽快考虑了)
脚本原执行时间43秒,引入5个worker 的多线程laungcher后为12秒,至少提高50%!!
最简单的Python 多线程模型:
#!/usr/bin/env python
# encoding: utf-8
"""
discovery.py
Created by wangchen on 2010-06-25.
Copyright (c) 2010 NetQin. All rights reserved.
"""
import sys
import os
import threading
from xml.dom import minidom
from re import match
from time import time, strftime, localtime
class WorkThread(threading.Thread):
def __init__(self, func, targets):
"""docstring for __init__"""
threading.Thread.__init__(self)
self._func = func
self._targets = targets
def run(self):
"""docstring for run"""
print 't'.join([self.getName(), str(len(self._targets))])
for target in self._targets:
self._func(target)
def laungcher(thread_num, target, func, pattern=None):
if os.path.isdir(target):
cmd = 'find "' + target + '" -type f'
if not pattern == None: cmd += ' -regex "' + pattern + '"'
targets = os.popen(cmd).readlines()
total = len(targets)
pos = 0
wndsize = int(round(float(total) / thread_num))
workers=[]
while (pos < total):
end = pos + wndsize
mt = WorkThread(func, targets[pos:end])
mt.start()
#mt.join()
workers.append(mt)
pos = end
for worker in workers:
worker.join()
else:
func(target)
def proc_report(target):
"""docstring for proc_report"""
pass
def main():
t = time()
if len(sys.argv) < 2:
print 'Usage: ', sys.argv[0], ''
sys.exit()
laungcher(5, sys.argv[1], proc_report, '.*2_.*.xml')
print 'time used: ', time() - t
if __name__ == '__main__':
main()
更多:python字典的key以是列表Python 多线程
https://www.002pc.comhttps://www.002pc.com/python/2727.html
你可能感兴趣的多线程,Python
php zbarcode扩展PHP使用Pthread实现的多线程操作实例
本文实例讲述了PHP使用Pthread实现的多线程操作。分享给大家供大家参考,具体如下:
电脑如何调屏幕亮度Linux最大线程数限制及当前线程数查询
Linux最大线程数限制及当前线程数查询,有需要的朋友可以参考下。
False
PHP的线程安全与非线程安全版本的区别
Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?
手机360总重启电脑系统更新Solaris线程模型详解
计算机中正在执行的程序称为进程,进程中单一顺序的控制流叫做线程,进程是资源管理的最小单位,线程是程序执行的最小单位。在操作系统设计上,从进程演化出线程,最主要的目的就是利
linux 查看某进程的线程MongoDB索引实战技巧
索引类型第二电脑网从linux 查看某进程的线程MongoDB索引实战技巧分析来看,对linux 查看某进程的线程MongoDB索引实战技巧的结果。虽然MongoDB的索引在存储结构上都是一样的,
0踩
赏
0 赞