Django视图返回在多进程上阻塞的Httpresponse

2024-10-02 00:30:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个简单的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)

新的背后_关税.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应该已经返回,但这不是我看到的。你知道吗

感谢您的帮助!谢谢。你知道吗


Tags: ipinfoidjsonnewloggingtypemapping

热门问题