-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuntitled0.py
50 lines (39 loc) · 1.12 KB
/
untitled0.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import multiprocessing as mp,os
import time
t0 = time.time()
text = []
def process_wrapper(lineID):
with open("vocab.kos.txt") as f:
for i,line in enumerate(f):
if i != lineID:
continue
else:
return line
break
def chunkify(fname,size=100):
fileEnd = os.path.getsize(fname)
with open(fname,"rb") as f:
chunkEnd = f.tell()
while True:
chunkStart = chunkEnd
f.seek(size,1)
f.readline()
chunkEnd = f.tell()
yield chunkStart, chunkEnd - chunkStart
if chunkEnd > fileEnd:
break
if __name__ == '__main__':
#init objects
pool = mp.Pool(4)
jobs = []
#create jobs
for chunkStart,chunkSize in chunkify("vocab.kos.txt"):
print((chunkStart,chunkSize))
jobs.append( pool.apply_async(process_wrapper,(chunkStart,chunkSize)) )
#wait for all jobs to finish
for job in jobs:
text.append(job.get())
#clean up
pool.close()
t1 = time.time()
total = t1-t0