python - Handle Django low responsiveness -


i'm having issues bad responsiveness in django app not know how explain. i'm using nginx, uwsgi django 1.7 , python3.4 within cent os dev environment. there couple of background cpu intense computing threads run periodically performing tasks (calculations, db operations, network i/o) . large task handle, notice cpu utilization grows 100% longer time. during high cpu utilization , django request takes lot of time handle, resulting in timeout due nginx.

what find hard understand why django thread handling request gets such small cpu slice ? it's background tasks hog processor while other threads leftovers.

given high cpu load, mechanism / options available have django perform responsively ?

it sounds need adjust scheduling priority of background operations. if you're not familiar term, here basic idea: in order run many processes, operating system has divide cpu time little slices, , assign each running process slice. every time "time slice" ends, os picks new process run in next time slice. choice called cpu scheduling, , occurs many times each second.

most operating systems allow adjust priority of process. in essence, gives os "hint" should schedule process more or less frequently, respect other running processes. linux allows adjust priority of process setting it's "nice" value. nice values range -20 (the highest priority/most scheduled) 19 or 20 (the lowest priority). (it's kind of backwards you'd expect).

you can set process's nice value in couple ways. when start it, can run this: nice -n [value] [program name]. if process running, can run renice -n [value] [pid], pid process id. process can adjust own nice value using nice(3) system call.

so, in mind, want one of these things:

  • increase priority (lower nice value) of processes need responsive (i.e. nginx, uwsgi, django).
  • decrease priority (increase nice value) of background processes (i.e. calculations, database indexing, etc).

the cleaner thing decreasing priority of background processes, since there fewer of "responsive" processes. if background processes in python, can make use of os.nice() function adjust own priority. otherwise, program may have configuration option specify nice value. if neither of works, you'll have renice them, , you'll have each time reboot well.

of course, if you'd like, increase priority of nginx adjusting configuration, uwsgi (i don't know how -- maybe configuration, otherwise renice), , django app (probably os.renice()).

whatever do, keep following in mind:

  • you need root increase priority (decrease nice), not opposite. point in favor of decreasing priority of background processes.
  • don't go overboard nice values! can @ various nice values of processes running on system using top or htop (it's "ni" column). need set numbers process want running more has little bit lower of nice value other one.

hopefully helps. more information readily available in man pages: nice(1), renice(1), nice(3), sched(7). or google "linux nice" or similar. have fun!


Comments