重定向django内部的超时和性能问题

时间:2017-08-10 11:27:48

标签: python django redirect timeout

我目前在Django重定向上遇到Timeout和性能问题。在我使用2台设备冲浪到本地托管的应用程序并且在我的localhost上只启用了一个工作程序,超时设置为30秒之前,这个问题是不可见的。

我有一个基于给定URL的页面重定向的views.py函数。我在表中查找pk并返回url。我还有一个计数器,可以跟踪前锋的数量。

urls.py:

url(r'^i/(?P<pk>[-\w]+)/$', frontendapp_views.item_view, name="item_view"),

页面立即重定向到“desired_url_forward”,但是,连接保持与用户打开,而实际上,用户已离开我的Django环境。这不知何故让我的工作人员等待30秒,而我已经转发到外部页面,不允许与一名工人处理任何其他请求。

我可以增加工作人员数量或缩短超时时间,但这感觉不对,因为它没有解决核心问题。

这是我在这个主题上发现的唯一一件事,但我对此并不熟悉:https://github.com/requests/requests/issues/520

这就是views.py的样子:

def item_view(request,pk):
    pk_binairy = urlsafe_base64_decode(pk)
    pk_int = int.from_bytes(pk_binairy, byteorder='little')
    desired_url_forward_object = get_object_or_404(forwards,pk = pk_int)
    channel_cleaned_utm = re.sub(' +',' ',"".join([request.GET.get('utm_source', ''),' ',request.GET.get('utm_medium', ''),' ',request.GET.get('utm_campaign', ''),' ',request.GET.get('utm_term', ''),' ',request.GET.get('utm_content', '')]))
    channel_cleaned = request.META.get('HTTP_REFERER')
    if channel_cleaned is None:
        channel_cleaned = 'Direct Traffic'
    visitor_ip_request = get_client_ip(request)
    location_request = get_client_location(request, visitor_ip_request)
    clickstat = clickstats(
        urlid = pk_int,
        user = desired_url_forward_object.user,
        channel = channel_cleaned,
        visitor_ip = visitor_ip_request,
        city = location_request['city'],
        region = location_request['region'],
        country = location_request['country'],
        device_type =  request.user_agent.device.family,
        browser = request.user_agent.browser.family,
        browser_version = request.user_agent.browser.version_string,
        operating_system = request.user_agent.os.family ,
        operating_system_version = request.user_agent.os.version_string
        )
    clickstat.save()

    if desired_url_forward_object.counterA <= desired_url_forward_object.counterB:
        desired_url_forward = desired_url_forward_object.urlA
        desired_url_forward_object.counterA = F('counterA') + 1
    else:
        desired_url_forward = desired_url_forward_object.urlB
        desired_url_forward_object.counterB = F('counterB') + 1
    desired_url_forward_object.save()
    return redirect(desired_url_forward)

有人建议吗?谢谢你的帮助!

0 个答案:

没有答案