Я создал следующие вспомогательные методы, которые помогут мне в этом:
Код: Выделить всё
#get a list of positions so that the file can be divided to chunks of chunksize
def getpos(fnom, chunksize):
pos = []
curr = 0;
read = 0
f = open(fnom, 'r')
while True:
ch = f.read(1)
curr = curr+1
read = read +1
if not ch:
pos.append(curr-1)
break
if(read >= chunksize):
pos.append(curr)
read = 0
return pos
#find recurrences of b in chunks of the file, starting at pos and with a max size of each chunk being csize
def counter(f, pos, csize, b):
sub = ''
curr = 0
end = pos+csize
f = open(f, 'r')
while True:
ch = f.read(1)
curr = curr+1
#print(curr)
if not ch:
break
if(curr >= pos and curr < end):
sub += ch
recurr = sub.count(b)
return recurr
Будет ли это работать:
Код: Выделить всё
def total_counter(fnom, b):
threads = []
pos = getpositions(fnom, 8)
for i in pos:
t = threading.Thread(target=chunk_counter, args=(fnom, i, 8, b))
threads.append(t)
t.start()
Подробнее здесь: https://stackoverflow.com/questions/436 ... -in-a-file