我遇到了一種我從未見過的奇怪的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