-
Notifications
You must be signed in to change notification settings - Fork 0
/
spawn.py
124 lines (85 loc) · 3.56 KB
/
spawn.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import subprocess
import sys
import os
import time
import random
subprocess.run([sys.executable, "-m", "pip","install","cython"])
subprocess.run([sys.executable, "cythonize.py"])
from google.cloud import storage
client = storage.Client()
def upload_data_to_gcs(data, target_key, **kwargs):
bucket = client.bucket(bucket_name)
bucket.blob(target_key).upload_from_string(data, **kwargs)
return bucket.blob(target_key).public_url
import unicodedata, re
def slugify(value):
value = unicodedata.normalize('NFKD', value.split('/')[-1].split('.')[0])
value = (re.sub(r'[^\w\s-]', '', value).strip().lower())
value = (re.sub(r'[-\s]+', '-', value))
return value
##########################################
#### ####
#### Arguments passed to this scripts ####
#### ####
##########################################
bucket_name = sys.argv[1]
#############################################
## ##
## Get the filenames that are complete ##
## ##
#############################################
masked = list(client.list_blobs(bucket_name, prefix='masked_data2/data_'))
masked = [m.name for m in masked]
###########################################
## ##
## Get the data that need processing ##
## ##
###########################################
blobs = []
for blob in client.list_blobs(bucket_name, prefix='public_model/corpus_only/'):
if(blob.name.endswith('.txt')):
blobs.append(blob)
#####################################
## ##
## Set env variables if needed ##
## ##
#####################################
my_env = os.environ.copy()
my_env['TOKENIZERS_PARALLELISM']='true'
for blob in blobs:
fn = blob.name
_fn = slugify(fn)
try:
#####################################
## ##
## Here is the lock logic ##
## Go next if the job is locked ##
## ##
#####################################
upload_data_to_gcs("0", "masked_data2_working/%s"%_fn, if_generation_match=0)
# ensure just one worker do the job at the same time.
except:
continue
#######################################################
## ##
## Check if the processed data are already there ##
## ##
#######################################################
if ("masked_data2/data_original_%s"%_fn in masked and
"masked_data2/data_masked_%s"%_fn in masked and
"masked_data2/data_labels_%s"%_fn in masked
):
continue
t0 = time.time()
###############################################
## ##
## Run the script to do the actual work! ##
## ##
###############################################
process = subprocess.Popen((sys.executable+' convert_to_bytes.py %s %s'%(bucket_name, fn)).split(' '), env=my_env, stdout=subprocess.PIPE)
for line in iter(process.stdout.readline, b''):
print(line.decode().strip())
t1 = time.time()
MB = blob.size / 1024 / 1024
print(f"** finish processing file {blob.name} in %.4f, speed: %.4f MB / s"%((t1-t0), MB / (t1-t0)))
print()