2

I'm new to django context processors. I wanted to create something which would give me famous quotes in every page of my blog app. When I deployed it to Heroku it started giving errors few minutes after its launching. These errors were :

could not fork new process for connection: Cannot allocate memory
SSL SYSCALL error: EOF detected
FATAL:  out of memory
DETAIL:  Failed on request of size 112.

Then the app restarts - back to normal. I could generate the same with javascript but still I wanted to do it with a context processor.

Is it advisable to create a context processor like one given below ?

from blog.models import Post

from random import randrange

def recent_posts(request):
    u = Post.objects.all()[:5]

    return {
        'recent_posts': u
    }


def quotes(request):
    var = randrange(3)
    quotes = {
        '0' : "Quote 1",
        '1' : "Quote 2",
        '2' : "Quote 3",
    }

    quoted_by = {
        '0' : "Person 1",
        '1' : "Person 2",
        '2' : "Person 3",   
    }

    return {
        'quotes': quotes[str(var)],
        'quoted_by': quoted_by[str(var)]
    }
madil
  • 35
  • 6
  • 2
    It looks like error and your processor are not related. Have you checked with the Heroku docs and/or guys?. – n3storm Mar 25 '13 at 08:07
  • 1
    if you have your recent_posts in your base page (in a widget), using this context processor is cool, same for "quotes" if you want your site to be usable by JavaScript disabled browsers. Your out of memory error is probably thrown in reaction to hardware limitations. (RAM full) you may want to profile your request localy with django debug toolbar and see your biggests orm requests. – christophe31 Mar 25 '13 at 08:07
  • Is quotes function the exact function you are using? I think randrange can be returning a '3' and you don't have 4 quotes. – n3storm Mar 25 '13 at 08:10
  • @n3storm : ive seen about this error. its related to psycopg2 and postgres DB. But why does it happen only when i add this context processor? And randrange(3) returns - 0,1 and 2 only. – madil Mar 25 '13 at 08:42
  • @christophe31 Let me check. Should I be using [this](https://github.com/django-debug-toolbar/django-debug-toolbar) tool from github ? – madil Mar 25 '13 at 08:44
  • @medil : this is The tool for dev/debuging. you can't dev efficiently without. by experiences, i often divide by 10 the loadtime with some "select_related" and to know which model i need to "related", just use django_debug_toolbar – ornoone Mar 25 '13 at 08:55
  • @ornoone using it locally. No RAM full/errors nor my sql queries are taking too much time. I will be checking it on Heroku for hardware failures. – madil Mar 25 '13 at 09:15
  • Aah found it : When I get that error, CPU time usage shows : Context switches - 189 voluntary, 503 involuntary and normally its like : Context switches 16 voluntary, 21 involuntary – madil Mar 25 '13 at 09:53
  • 1
    @madil well played, good luck/work to continue profiling your app. – christophe31 Mar 25 '13 at 10:07
  • @christophe31 It still has those errors but now i know how i get it. I've another question [see to it](http://stackoverflow.com/questions/15613660/page-refresh-increases-request-size-cpu-time-and-context-switches-in-django-wh). – madil Mar 25 '13 at 11:24

0 Answers0