我正在制作一个简单的Django JSON REST API:
@require_http_methods(["POST"])
@csrf_exempt
def upload_tariff(request, tariff_name, year, month, day, traffic_type):
new_tariff = Tariff(
name=tariff_name,
date=datetime_dt,
tariff_type=default_tariff_type,
traffic_type=traffic_type,
tariff_file=uploaded_file,
max_loss=None
)
logging.info('------------------Before saving---------------------')
new_tariff.save()
logging.info('******************After saving***********************')
updateobject_id = new_tariff.updateobject_set.all()[0].pk
logging.info('-----------------updateobject id %s--------------' % updateobject_id)
mapping_json = {
'mapping': {
mapping_obj.switch.node: mapping_obj.i_tariff
for mapping_obj in new_tariff.tariffmapping_set.all()
}
}
logging.info('----------------mapping %s----------' % mapping_json)
mapping_json.update({'updateobject_id': updateobject_id})
return HttpResponse(json.dumps(mapping_json), content_type='application/json', status=200)
在new_tariff.save()后面是使用多进程运行一段时间的5个子进程。因此,我使子进程无阻塞,即我没有加入它们。但是,当我触发post请求时,仍然需要一段时间才能获得响应,这意味着响应在子进程上阻塞以完成。现在看一下日志:
Jul 4 21:30:27 ip-xx-x-x-xx ------------------Before saving---------------------
Jul 4 21:30:29 ip-xx-x-x-xx ******************After saving***********************
Jul 4 21:30:29 ip-xx-x-x-xx -----------------updateobject id 264--------------
Jul 4 21:30:29 ip-xx-x-x-xx ----------------mapping {'mapping': {1: 964, 2: 700, 3: 696, 4: 771, 5: 693}}----------
Jul 4 21:30:41 ip-xx-x-x-xx subprocess 2 started
Jul 4 21:30:42 ip-xx-x-x-xx subprocess 4 started
Jul 4 21:30:51 ip-xx-x-x-xx subprocess 1 started
Jul 4 21:30:52 ip-xx-x-x-xx subprocess 5 started
Jul 4 21:30:55 ip-xx-x-x-xx subprocess 3 started
显然,应该返回一个HttpResponse,但这不是我所看到的。
任何帮助表示赞赏!感谢。