我需要一些关于性能调整的建议这个脚本读取由临时内存磁盘上的代理转储的URL头,它读取文件并将其附加到列表中,在一些检查后,它读取列表,如果行包含"User-Agent“编辑,并使用标准输出清除...
proc = open(sys.argv[1],'r')
slog.write("writing standard input \n")
for line in proc.readlines():
header.append(line)
. . . . . . . .
if check_header == None: #check_header is returned by one of the functions to whether rewrite the header
for h in header:
if "User-Agent" in h and "custom-header:" not in h:
h = h.rstrip("\r\n") + " custom-header:" + customer + "\r\n"
sys.stdout.write(h)
sys.stdout.flush()
#sys.exit(1)
else:
sys.stdout.write(new_get)我担心的是,对于大量的请求,它将是缓慢的,因为它附加到列表,读取和刷新它,任何想法,我可以如何性能调整它
发布于 2012-10-12 02:52:50
除非代码示例中的缩进是错误的,否则您尝试添加自定义标头的次数与列表中的元素一样多。试一试
proc = open(sys.argv[1],'r')
slog.write("writing standard input \n")
for h in proc.readlines():
. . . . . . . .
if check_header == None: #check_header is returned by one of the functions to whether rewrite the header
if "User-Agent" in h and "custom-header:" not in h:
h = h.rstrip("\r\n") + " custom-header:" + customer + "\r\n"
sys.stdout.write(h)
sys.stdout.flush()
else:
sys.stdout.write(new_get)https://stackoverflow.com/questions/12846253
复制相似问题