-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconcurrent_gzip_append.py
37 lines (29 loc) · 1014 Bytes
/
concurrent_gzip_append.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
import gzip
import fcntl
from joblib import Parallel, delayed
from tqdm import tqdm
M = 4096*4
N = 4096*4
D = 100
s = '11 '
def write_matrices(path, M, N):
with gzip.GzipFile(path, 'wb') as f:
for i in range(M):
f.write((s * N + '\n').encode('utf-8'))
input_paths = ['/tmp/mn-{}'.format(i)
for i in range(D)]
Parallel(n_jobs=-1)(delayed(write_matrices)(p, M, N)
for p in tqdm(input_paths))
def append_to_file(input_path, paths):
with gzip.GzipFile(input_path, 'rb') as f:
for p, l in zip(paths, f):
with gzip.GzipFile(p, 'ab') as ofile:
fcntl.flock(ofile, fcntl.LOCK_EX)
ofile.write(l)
fcntl.flock(ofile, fcntl.LOCK_UN)
output_paths = ['/tmp/nd-{}'.format(i) for i in range(M)]
Parallel(n_jobs=-1)(delayed(append_to_file)(p, output_paths)
for p in tqdm(input_paths))
for p in tqdm(output_paths):
with gzip.GzipFile(p, 'rb') as f:
r = f.read()