Django broken pipe error

dr.roman.graf at gmail.com dr.roman.graf at gmail.com
Wed Dec 7 04:08:03 EST 2016


On Tuesday, December 6, 2016 at 3:22:13 PM UTC+1, dr.rom... at gmail.com wrote:
> Hi,
> 
> I'm facing strange Django broken pipe error (Python 2.7 on Ubuntu) that apparently is a not fixed Django bug. Does anybody now how to fix it? I've been searching a lot and didn't find any solution.
> 
> This error happens very irregularly by Post request in Django. Sometimes it works sometimes not: 
> 
> [06/Dec/2016 13:33:57] "POST /export_report/ HTTP/1.1" 500 59
> Traceback (most recent call last):
>   File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
>     self.finish_request(request, client_address)
>   File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
>     self.RequestHandlerClass(request, client_address, self)
>   File "/home/ait/.virtualenvs/env1/local/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 129, in __init__
>     super(WSGIRequestHandler, self).__init__(*args, **kwargs)
>   File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
>     self.finish()
>   File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
>     self.wfile.close()
>   File "/usr/lib/python2.7/socket.py", line 279, in close
>     self.flush()
>   File "/usr/lib/python2.7/socket.py", line 303, in flush
>     self._sock.sendall(view[write_offset:write_offset+buffer_size])
> error: [Errno 32] Broken pipe
> ----------------------------------------
> Exception happened during processing of request from ('127.0.0.1', 38224)
> 
> It is also described in: https://www.reddit.com/r/joinmarket/comments/4atqrm/is_this_exception_normal_exception_happened/
> 
> On https://bugs.python.org/issue14574 is stated that this error should already be fixed but apparently not.
> 
> Best regards,
> 
> Roman

Thank you Justin,

I'm on the dev server and should present results in this way. 

Yes, I use manage.py runserver --insecure to start the server (from PyCharm).

My views.py call:

@detail_route(methods=['post'], permission_classes=[permissions.AllowAny])
def export_report(request):
    body_unicode = request.body.decode('utf-8')
    body_str = body_unicode.encode('ascii','ignore')
    attr_list = body_str.split('&')
    attr_dict = {}
    if (len(attr_list) > 0):
        for attr in attr_list:
           ...
           key = key_value_pair[0]
           attr_dict[key] = key_value_pair[1]
    trend = trends.calculate_trend(
        attr_dict['search_phrase']
        , attr_dict['time_from']
        , attr_dict['time_to']
        , attr_dict['time_scale']
    )
    attr_dict['trend'] = trend
    res = str(json.dumps(attr_dict))
    return HttpResponse(res, content_type="text/plain; charset=utf-8")

and trend calculation in trends.py with database calls:

def calculate_trend(query_phrase, time_from, time_to, time_scale):
    # check in database if trend already exists
    try:
        db_trend = Trend.objects.get(pk=query_phrase)
        if db_trend.from_time.strftime("%Y-%m-%d") == time_from \
                and db_trend.to_time.strftime("%Y-%m-%d") == time_to \
                and db_trend.granularity == time_scale:
            logger.info("trend already exists.")
            existing_trend_dict = ast.literal_eval(db_trend.content)
            return existing_trend_dict
    except Trend.DoesNotExist:
        logger.info("It is a new trend search.")
    trend_dict = {}
    start_time = pd.Timestamp(value[0])
    end_time = pd.Timestamp(value[-1])
    freq = ... get frequency using pandas lib
    trend_dict[key] = freq
    json_trend_content = trend_dict_to_sorted_json_str(trend_dict)
    trend = Trend(
        phrase=query_phrase,
        content=json_trend_content,
        from_time=time_from,
        to_time=time_to,
        granularity=time_scale,
    )
    if trend is not None:
        try:
            db_trend = Trend.objects.get(pk=query_phrase)
            db_trend.delete()
            logger.info("delete old trend: %s. " % trend)
        except Trend.DoesNotExist:
            logger.info("create trend: %s. " % trend)
        trend.save()
    return trend_dict

Thank you in advance!

Roman


More information about the Python-list mailing list