我遇到了一种我从未见过的奇怪的Python行为。 我运行下面的代码:Python完成脚本后挂起
from __future__ import print_function, division
import itertools
import sys
R1_file = sys.argv[1]
R2_file = sys.argv[2]
out_stats = sys.argv[3]
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
args = [iter(iterable)] * n
return itertools.izip_longest(fillvalue=fillvalue, *args)
print('### Started preparing iterators')
# iterate on reads files and get vector of locations per sequencse
fid1 = open(R1_file)
fid2 = open(R2_file)
gr1 = grouper(fid1,4)
gr2 = grouper(fid2,4)
together = itertools.izip(gr1,gr2)
seq_hash = {}
seq_ind = 0
print('### Started reading fastq')
for blocks in together:
seq_ind += 1
if seq_ind%1000000 == 0:
print('Read in',seq_ind,'reads')
s1 = blocks[0][1]
s2 = blocks[1][1]
conc_seq = s1.strip()+s2.strip()
if conc_seq in seq_hash:
seq_hash[conc_seq].append(seq_ind)
else:
seq_hash[conc_seq] = [seq_ind]
fid1.close()
fid2.close()
# print results to file
print('### Started writing results to file')
with open(out_stats,'w') as fo:
for seq,locations_vec in seq_hash.iteritems():
n = len(locations_vec)
if n > 1:
print(seq,n,':'.join(str(l) for l in locations_vec),sep='\t',file=fo)
print('done writing to file')
print('FINISHED')
运行此脚本两个FASTQ文件,其中有一个特定的格式,查找重复的数据,并产生一些统计数据。现在,奇怪的是,脚本运行完毕后,也就是说,所有需要的统计信息都被打印到输出文件中,并且'FINISHED'被打印到STDOUT,剧本只是唠叨,几乎看不到任何东西!延迟的持续时间取决于输入文件的大小:当我给100M输入时,它会挂起几秒钟,当输入是500M文件时,它会挂起大约10分钟,当我运行全部数据时 - 〜 130G - 它几乎没有结束(我跑了一夜,它没有完成)。同样,需要写入输出和标准输出的所有内容都确实写入了。在延迟时间内,CPU使用率很高,保存数据所需的内存仍然被占用。我尝试使用pdb做一些跟踪,看起来好像脚本已经在打印'FINISHED'(尽管我可能会解释pdb输出错误)之后一次又一次地以for blocks in together:
开始的循环运行。
目前我只要终止脚本,只要它进入滞后阶段,然后可以在没有问题的情况下使用输出,但这仍然很烦人。 我在Ubuntu上运行,使用Python 2.7。
任何想法?
通过文件大小来看,我认为蟒蛇是在年底做清洁工作'FINISHED'后面的脚本。由于您将所有内容存储在字典seq_hash中,因此它将变得非常大。 – gdlmx
你也可以在这里发布你的'pdb'输出吗? – gdlmx
@gdlmx - 我真的不能把它放在这里,因为它太大了。基本上,它只是一遍又一遍地打印相同的代码块(我提到的循环)。当我运行全部数据时(我正在使用高内存的机器),内存使用量大约为200G。我不认为应该花几个小时从RAM中删除一个散列...... – soungalo