4

I'm working with django and doing facebook integration for which is needed a test server. I've had lots of problems with Apache and its caching of .pyc files, I even asked here on stackoverflow.

That solution works but I want to know if there is an option for disabling Apache caching of such files. Server restart might be a problem for me.

EDIT:

Here is the django.wsgi code:

path = '/not/actual/path'
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'notactualproj.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Community
  • 1
  • 1
prototype
  • 3,303
  • 2
  • 27
  • 42

1 Answers1

8

How to solve your "Bytecode problem"

You should probably figure out why those unwanted .pyc files are there in the first place (are these in your repository? They should be ignored).

As mentionned in the comments, if you have dangling .pyc files that cause issues, you could incorporate removing all the .pyc files as part of your pull process when you deploy newer code to the server. Running the app will re-create the ones that are needed when the modules are imported.


Now, if you really don't want to have bytecode generated, you could use the PYTHONDONTWRITEBYTECODE environment variable, but I wouldn't recommend that as it seems a pretty excessive solution.

How to solve Apache seemingly pulling older versions of the code.

Now, you have to make the difference between two problems at play here.

  • Older Bytecode files generated by python (e.g. .pyc files), which can cause an issue in specific cases like replacing a file with a module, but are not often a cause for concern.
  • Mod WSGI not reloading newer code that gets uploaded. This depends on which mode you're running Mod WSGi in, and an usual symptom is that hitting a page seems to randomly pull the newer or older version of the code.

To solve the first issue, you just have to remove unused bytecode files. But, again, this probably isn't what's causing your issue.

To solve the second issue, you have two solutions

  • Restarting apache when you upload newer code. Using apache2ctl -k graceful, this will be transparent to your users, and I can't see why "Server restart might be a problem", unless you're on shared hosting.
  • Using code reloading, you might want to have a look at the mod_wsgi documentation.

I don't think bytecode is your issue, and code reloading probably is.

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • So are you proposing that I should delete these files from my repo and my local folders connected to that repo? I don't know why they are there - I noticed them as soon as these problems started to emerge. – prototype Oct 01 '12 at 11:07
  • What I mean is that those `.pyc` files should not be in repository itself. They will be generated on the fly when code is run out of the `.py` files. It is, however, true, that an unwanted `.pyc` file could stay in your tree after the `.py` file it was generated from is removed and cause issues (like you're converting a file to a module). You could add purging your tree as a step after pulling changes - necessary `.pyc` files will be re-created automatically. – Thomas Orozco Oct 01 '12 at 11:10
  • Well I removed all `.pyc` files from my local folders, repo, test server folders and restarted apache. Still the problem persists. – prototype Oct 01 '12 at 13:10
  • But what is the actual problem? – Thomas Orozco Oct 01 '12 at 14:28
  • Different error appears every time I refresh the page. I'm certain that these errors are fixed. Here is an example: I have (intentionally) left out HttpResponse at the end of a view. I commited the change, fixed it and committed it again (I'm doing svn up all the time on the test server). Now that mistake should not appear again but it does sometime appear for some reason. – prototype Oct 01 '12 at 14:42
  • What setup are you using? (`mod_wsgi`?) And what makes you think it has to do with the `.pyc` files? – Thomas Orozco Oct 01 '12 at 14:47
  • Yes, I'm using the mod_wsgi setup. – prototype Oct 01 '12 at 14:48
  • I inserted the django.wsgi code in the question above. It looked really bad here in the comments. – prototype Oct 01 '12 at 14:50
  • Well I've asked a question similar to this -> [link](http://stackoverflow.com/questions/12578602/django-project-and-svn-loads-older-code-versions/12578673#12578673) and the answer was that the .pyc files were making the problem. – prototype Oct 01 '12 at 14:54
  • I don't think `.pyc` files are causing you issue. Additionnally, `.pyc` files have nothing to do with caching or with Apache. They are generated by python itself and are the bytecode-compiled version of your `.py` files. See my edited answer for a more detailed explanation on how to solve your actual issue. – Thomas Orozco Oct 01 '12 at 16:51
  • @dark4p Did you actually read the article on source code reloading on the mod_wsgi site you were referred to? When you push up code changes, you are going to have to do something to force existing running processes to reload code. It does not work the same as PHP where any code changes is automatically reloaded. – Graham Dumpleton Oct 03 '12 at 01:11