-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (52 loc) · 1.7 KB
/
app.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
import time
import random
import string
import multiprocessing as mp
import pprint
import pickle
import signal
def sigterm_handler(signal, frame):
print("guardando logs y otras cosas")
signal.signal(signal.SIGTERM, sigterm_handler)
DURACION = 60
def random_word(length):
'''devuelve palabras con numero y letra'''
word = string.ascii_letters + string.digits
return ''.join(random.choice(word) for i in range(length))
def add_word(star_time, word_dict):
'''proceso que añade palabras al diccionario compartido entre procesos
durante un tiempo'''
diff_time = 0
while(diff_time < DURACION):
time_now = time.time()
diff_time = time_now - star_time
print(diff_time)
word = random_word(5)
if word in word_dict.keys():
word_dict[word] =+ 1
else:
word_dict[word] = 0
#pprint.pprint(word_dict.keys())
if __name__ == '__main__':
start_time = time.time()
manager = mp.Manager()
word_dict = manager.dict()
N_PROCESSES = 3
pool = mp.Pool(N_PROCESSES)
pool.apply_async(add_word, args = (start_time, word_dict))
pool.close()
pool.join()
pprint.pprint(word_dict.values())
elementos = len(word_dict)
repetidos = 0
for key in word_dict.keys():
if word_dict[key] > 0:
reetidos =+ 1
diferentes = elementos - repetidos
''' salvar '''
with open(pickefile, 'wb') as f:
pickle.dump(word_dict,f)
print(" -------------------------------------- ")
print(f" ---- elementos --- {elementos}---- ")
print(f" ---- repetidos --- {repetidos}---- ")
print(f" ---- diferentes --- {diferentes}---- ")