#flask
#flask
Вопрос:
Я пытаюсь заставить код slurm-web работать. в restapi.py , существует метод def sinfo(), который выглядит следующим образом:
@app.route('/sinfo', methods=['GET', 'OPTIONS'])
@crossdomain(origin=origins, methods=['GET'],
headers=['Accept', 'Content-Type', 'X-Requested-With', 'Authorization'])
@authentication_verify()
def sinfo():
# Partition and node lists are required
# to compute sinfo informations
partitions = get_from_cache(pyslurm.partition().get, 'get_partitions')
nodes = get_from_cache(pyslurm.node().get, 'get_nodes')
# Retreiving the state of each nodes
nodes_state = dict(
(node.lower(), attributes['state'].lower())
for node, attributes in nodes.iteritems()
)
# For all partitions, retrieving the states of each nodes
sinfo_data = {}
for name, attr in partitions.iteritems():
for node in list(NodeSet(attr['nodes'])):
key = (name, nodes_state[node])
if key not in sinfo_data.keys():
sinfo_data[key] = []
sinfo_data[key].append(node)
# Preparing the response
resp = []
for k, nodes in sinfo_data.iteritems():
name, state = k
partition = partitions[name]
avail = partition['state'].lower()
min_nodes = partition['min_nodes']
max_nodes = partition['max_nodes']
total_nodes = partition['total_nodes']
job_size = "{0}-{1}".format(min_nodes, max_nodes)
job_size = job_size.replace('UNLIMITED', 'infinite')
time_limit = partition['max_time_str'].replace('UNLIMITED', 'infinite')
# Creating the nodeset
nodeset = NodeSet()
map(nodeset.update, nodes)
resp.append({
'name': name,
'avail': avail,
'job_size': job_size,
'time_limit': time_limit,
'nodes': total_nodes,
'state': state,
'nodelist': str(nodeset),
})
# Jsonify can not works on list, thus using json.dumps
# And making sure headers are properly set
return make_response(json.dumps(resp), mimetype='application/json')
в журнале ошибок apache указано, что
возвращает make_response(json.dumps(соответственно), mimetype=’application/json’)
Ошибка типа: make_response() получил неожиданный аргумент ключевого слова ‘mimetype’
Я использую flase 1.0.2 и задаюсь вопросом, что вызывает эту ошибку.
Комментарии:
1. Сделайте это 3-шаговой операцией:
resp = make_response(json.dumps(resp)); resp.mimetype = 'application/json'; return resp
2. @Abdou Я последовал вашему комментарию и получил следующие ошибки: TypeError: не является сериализуемым в формате JSON. Если вы, пожалуйста, можете объяснить это поведение?
Ответ №1:
Во-первых, вам нужно сделать отступ, return
чтобы это произошло в конце sinfo()
. Затем вы можете упростить, написав
from flask import jsonify
...
def sinfo():
...
return jsonify(resp)