Is there any benefit to pyc files in a WSGI app where deployments happen several times per week?

StackOverflow https://stackoverflow.com/questions/13081659

  •  14-07-2021
  •  | 
  •  

Question

Seems like with ever increasing frequency, I am bit by pyc files running outdated code.

This has led to deployment scripts scrubbing *.pyc each time, otherwise deployments don't seem to take effect.

I am wondering, what benefit (if any) is there to pyc files in a long-running WSGI application? So far as I know, the only benefit is improved startup time, but I can't imagine it's that significant--and even if it is, each time new code is deployed you can't really use the old pyc files anyways.

This makes me think that best practice would be to run a WSGI application with the PYTHONDONTWRITEBYTECODE environment variable set.

Am I mistaken?

Was it helpful?

Solution

The best strategy for doing deployments is to write the deployed files into a new directory, and then use a symlink or similar to swap the codebase over in a single change. This has the side-benefit of also automatically clearing any old .pyc files.

That way, you get the best of both worlds - clean and atomic deployments, and the caching of .pyc if your webapp needs to restart.

If you keep the last N deployment directories around (naming them by date/time is useful), you also have an easy way to "roll back" to a previously deployed version of the code. If you have multiple server machines, you can also deploy to all of the machines but wait to switch them over until all of them have gotten the new code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top