-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
147 lines (116 loc) · 4.68 KB
/
worker.py
File metadata and controls
147 lines (116 loc) · 4.68 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# coding: utf-8
import logging
import argparse
import os
import socket
import string
import json
logging.basicConfig(level=logging.DEBUG,format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d %H:%M:%S')
logger = logging.getLogger('worker')
class Worker(object):
def __init__(self):
# -----------
# Set Worker pid, port and status
# -----------
self.id = os.getpid()
self.port = 8081
self.worker_status = "READY"
# -----------
# Worker Socket
# -----------
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# -----------
# Worker register msg with pid
# -----------
self.register_msg = json.dumps({
"task": "register",
"id": self.id
})
# -----------
# Bytes padding for msgs
# -----------
self.padding = 0
def handle_map_request(self, blob):
lista = []
punct = list(string.punctuation)
frase = blob.split()
lista_f = []
for palavra in frase:
palavra = ''.join(i for i in palavra if i not in punct and not i.isdigit())
for c in punct:
palavra = palavra.strip(c)
lista_f.append(palavra)
if palavra == '':
lista_f.remove(palavra)
for w in lista_f:
lista.append((w, 1))
# print()
# print(lista)
# print("------------------------------------------------------")
return json.dumps(dict(task="map_reply", value=lista))
def handle_reduce_request(self, value):
reduced_list = []
words = []
for nval in value:
for w, nr in nval:
w = w.lower()
if w not in words:
words.append(w)
reduced_list.append((w, nr))
else:
for i in reduced_list:
if i[0] == w:
reduced_list.remove((w, i[1]))
nr = nr + i[1]
reduced_list.append((w.lower(), nr))
# print()
# print(reduced_list)
# print("------------------------------------------------------------")
return json.dumps(dict(task="reduce_reply", value=reduced_list))
def main(self, args):
logger.debug('Connecting to %s:%d', args.hostname, args.port)
try:
self.sock.connect((args.hostname, args.port))
self.sock.sendall(self.register_msg.encode("utf-8"))
while True:
bytes_size = self.sock.recv(8).decode()
xyz = int(bytes_size)
json_msg = self.sock.recv(xyz).decode("utf-8")
if json_msg:
msg = json.loads(json_msg)
if msg["task"] == "map_request":
map_reply = self.handle_map_request(msg["blob"])
logger.debug('Handle Map Request')
print(map_reply)
size = len(map_reply)
self.sock.sendall((str(size).zfill(8) + map_reply).encode("utf-8"))
logger.debug('Send Map Reply')
if msg["task"] == "reduce_request":
reduce_reply = self.handle_reduce_request(msg["value"])
logger.debug('Handle Reduce Request')
print(reduce_reply)
size = len(reduce_reply)
self.sock.sendall((str(size).zfill(8) + reduce_reply).encode("utf-8"))
logger.debug('Send Reduce Reply')
if msg["task"] == "shutdown":
print("JOB COMPLETED WITH SUCCESS! >> SHUTDOWN")
break
except socket.error:
print("Error to connect with Coordinator")
finally:
self.sock.close()
def tokenizer(txt):
tokens = txt.lower()
tokens = tokens.translate(str.maketrans('', '', string.digits))
tokens = tokens.translate(str.maketrans('', '', string.punctuation))
tokens = tokens.translate(str.maketrans('', '', '«»'))
tokens = tokens.rstrip()
return tokens.split()
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='MapReduce worker')
parser.add_argument('--id', dest='id', type=int, help='worker id', default=0)
parser.add_argument('--port', dest='port', type=int, help='coordinator port', default=8765)
parser.add_argument('--hostname', dest='hostname', type=str, help='coordinator hostname', default='localhost')
args = parser.parse_args()
Worker().main(args)