I imagine that this is an area that hasn't really been given much
consideration with regards to optimization, because it isn't relevant to
normal Django deployments. However, with "serverless" deployments
(those without any permanent infrastructure), this becomes quite
relevant as we have to call setup() every request.
So, I'd love to discuss ideas for performance optimizations to Django's setup method. (A sample output for profile is available here: https://github.com/Miserlou/django-zappa/issues/24 )
@Aymeric
> In my opinion, the first concrete step would be to measure how much time is spent executing Django code rather than importing Python modules.
Over 70% of the total request time is spent in django.setup() - so you can see why we have an incentive to improve this!
You're halfway there, but the process is more complicated than that. The code is cached, not the internal state of the machine. You can follow our progress here: https://github.com/Miserlou/django-zappa/
But - another type caching could be another possibility. Can anybody think of any arrangements where we could perhaps call setup() with "pre-loaded"/cached applications?
Q: Will AWS Lambda reuse function instances?
To improve performance, AWS Lambda may choose to retain an instance of your function and reuse it to serve a subsequent request, rather than creating a new copy. Your code should not assume that this will always happen.
I always thought, and with very simple testing seen, that your code is basically "frozen" between each request, and for example, every import that is done in the main module is always done once (so your whole program is only initialized once) so this means django would only initialize once for quite a while (until your instance of the code is discarded, and a new request will basically generate all modules to be imported again). So technically if you do the right imports to have django call setup(), this should be only done once. What is really happening that makes it always call setup() on every request?
With the above said, if that's really the case. Python is known to able to serialize classes in a very interesting way (pickling) where you can even send a class with its methods over the wire and on the other side the person can execute every method defined in there and the class also keeps any state. Would it be possible to store the state with this somehow?
Ugh. As a strong advocate of both django and zappa, I'd love it if we could keep the conversation on target without degenerating into stack attacks. If you don't want to weigh in, please feel no obligation to do so.
I do agree that we could pare down the profile into more actionable sections. I'll see if I can get something a bit more preprocessed in the near future. Perhaps that will help keep this thread on target.
On 01 Mar 2016, at 01:44, Rich Jones <mise...@gmail.com> wrote:
I think the interesting things to explore would be:
- Loading apps in parallel
- "Pre-loading" apps before app deployment, then loading that cached state at runtime. I guess I'd need to know more about what it means to "load" an app to see if that makes any sense at all.
I imagine the former is probably more feasible. I understand the aversion to non-determinism, but I think that shouldn't matter as long as there is a way to define inter-app dependencies.