开发者

Using Django Caching with Mod_WSGI

开发者 https://www.devze.com 2023-04-06 04:01 出处:网络
Is anyone aware of any issues with Django\'s caching framework when deployed to Apache/Mod_WSGI? When testing with the caching framework locally with the dev server, using the profiling middleware an

Is anyone aware of any issues with Django's caching framework when deployed to Apache/Mod_WSGI?

When testing with the caching framework locally with the dev server, using the profiling middleware and either the FileBasedCache or LocMemCache, Django's very fast. My request time goes from ~0.125 sec to ~0.001 sec. Fantastic.

I deploy the identical code to a remote machine running Apache/Mod_WSGI and my request time goes from ~0.155 sec (before I deployed the change) to ~.400 sec (pos开发者_JS百科t deployment). That's right, caching slowed everything down.

I've spent hours digging through everything, looking for something I'm missing. I've tried using FileBasedCache with a location on tmpfs, but that also failed to improve performance.

I've monitored the remote machine with top, and it shows no other processes and it has 6GB available memory, so basically Django should have full rein. I love Django, but it's incredibly slow, and so far I've never been able to get the caching framework to make any noticeable impact in a production environment. Is there anything I'm missing?

EDIT: I've also tried memcached, with the same result. I confirmed memcached was running by telneting into it.


Indeed django is slow. But I must say most of the slowness goes from app itself.. django just forces you (bu providing bad examples in docs) to do lazy thing that are slow in production.

First of: try nginx + uwsgi. it is just the best.

To optimize you app: you need to find you what is causing slowness, it can be:

  • slow database queries (a lot of queries or just slow queries)
  • slow database itself
  • slow filesystem (nfs for example)

Try logging request queries and watch iostat or iotop or something like that.

I had this scenario with apache+mod_wsgi: first request from browser was very slow... then a few request from same browser were fast.. then if sat doing nothing for 2 minutes - wgain very slow. I don`t know if that was improperly configured apache if it was shutting down wsgi app and starting for each keepalive request. It just posted me off - I installed nging and with nginx+fgxi all was a lot faster than apache+mod_wsgi.


I had a similar problem with an app using memcached. The solution was running mod_wsgi in daemon mode instead of embeded mode, and Apache in mpm_worker mode. After that, application is working much faster.


Same thing happened to me and was wondering what is that is taking so much time. each cache get was taking around 100 millisecond.

So I debugged the code django locmem code and found out that pickle was taking a lot of time (I was caching a whole table in locmemcache). I wrapped the locmem as I didn't wanted anything advanced, so even if you remove the pickle and unpickle and put it. You will see a major improvement.

Hope it helps someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消