-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.py
More file actions
48 lines (38 loc) · 1.35 KB
/
worker.py
File metadata and controls
48 lines (38 loc) · 1.35 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
"""
worker:
A distributed machine executing computation missions
Receive code and data(with encryption)
hw 2019 1 24
"""
from flask import *
import importlib as imp
import json
app = Flask(__name__) # 创建1个Flask实例
workers_dict = {}
@app.route('/', methods=['POST', 'GET']) # 路由系统生成 视图对应url,1. decorator=app.route() 2. decorator(first_flask)
def index(): # 视图函数
if request.method == 'GET':
return render_template('worker.html', workers=workers_dict)
else: # 接收任务类型
print(request.form)
msgtype = request.form.get('type')
data = request.form.get('content')
try:
data = json.loads(data)
except:
return "error in decoding!"
if msgtype == "register": # 注册
already = []
for nid in data:
if nid in workers_dict:
already.append(nid)
workers_dict[nid] = data[nid]
if len(already) > 0:
return "替换的ID:" + "|".join(already)
else:
return "success"
if msgtype == "mission": # 分配任务
pass
return render_template('worker.html', workers=workers_dict)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001, debug=True) # 本地服务的端口 5001