Restarting a apache2 over perticular request in django - python

I am working a project, that creates an django app over a request. Since the webserver apache needs to be restarted to add new files created. I tried following:
1) Django request_complete Signals: This is not good way because I dont want my server to be restarting on every request complete
2) threading.Time:I tried to run a function with some delay so that it allows complete the current request.
This is not working giving following error:
RuntimeError at /apps/37f63340-2984-40b1-a728-1cf3d0820ae6/
threads can only be started once
Please suggest me way to solve this problem.

You tagged this with "webfaction" so I'm going to assume you're using WebFaction's default mod_wsgi setup.
If that's the case, then you can restart the mod_wsgi daemon processes, instead of restarting Apache itself, as described in the mod_wsgi documentation: Reloading Source Code

Related

Nginx - Seems to be running old python scripts

I have a website (panicselect.com), and I have made some changes to the python code which i pushed onto Github and then pulled onto my server, which seems to be successful. I have tried restarting the server, but it still seems to be running my older version of the code even though I have successfully pulled the new version. I believe this as some 'champions' are still missing and the rating seems to be calculated the old way, which is in contrast to what is on my localhost. Do you have an idea of what this could be? I'm running Ubuntu Linux on Digital Ocean with sendfile off and nginx uses Uwsgi to run the Py code.
To fully determine how to deploy changes to your production server, you must understand 2 things:
1. Most WSGI servers (including uWSGI) will load code on start, not on each execution.
That means, changes in your code won't be reflected immediately, because old code is still loaded into your WSGI server. It differs from PHP execution, where code is reloaded on every request. That means, you must restart your WSGI server when you want your new code to be loaded.
2. WSGI and nginx are NOT related
Yes, nginx will connect your WSGI server to outside world, but that's it! It doesn't manage your WSGI server. That means, you must restart your WSGI server by hand. Restarting nginx won't cause that.
Also it is good to note here: restarting nginx is not required, unless you've changed nginx configuration.

Django, RabbitMQ, & Celery - why does Celery run old versions of my tasks after I update my Django code in development?

So I have a Django app that occasionally sends a task to Celery for asynchronous execution. I've found that as I work on my code in development, the Django development server knows how to automatically detect when code has changed and then restart the server so I can see my changes. However, the RabbitMQ/Celery section of my app doesn't pick up on these sorts of changes in development. If I change code that will later be run in a Celery task, Celery will still keep running the old version of the code. The only way I can get it to pick up on the change is to:
stop the Celery worker
stop RabbitMQ
reset RabbitMQ
start RabbitMQ
add the user to RabbitMQ that my Django app is configured to use
set appropriate permissions for this user
restart the Celery worker
This seems like a far more drastic approach than I should have to take, however. Is there a more lightweight approach I can use?
I've found that as I work on my code in development, the Django
development server knows how to automatically detect when code has
changed and then restart the server so I can see my changes. However,
the RabbitMQ/Celery section of my app doesn't pick up on these sorts
of changes in development.
What you've described here is exactly correct and expected. Keep in mind that Python will use a module cache, so you WILL need to restart the Python interpreter before you can use the new code.
The question is "Why doesn't Celery pick up the new version", but this is how most libraries will work. The Django development server, however, is an exception. It has special code that helps it automatically reload Python code as necessary. It basically restarts the web server without you needing to restart the web server.
Note that when you run Django in production, you probably WILL have to restart/reload your server (since you won't be using the development server in production, and most production servers don't try to take on the hassle of implementing a problematic feature of detecting file changes and auto-reloading the server).
Finally, you shouldn't need to restart RabbitMQ. You should only have to restart the Celery worker to use the new version of the Python code. You might have to clear the queue if the new version of the code is changing the data in the message, however. For example, the Celery worker might be receiving version 1 of the message when it is expecting to receive version 2.

Django/Apache code not reloading after restart of web server

I'm dealing with legacy code on Ubuntu server. I've encountered something weird: after Apache restarts (a full server restart), the site is still using old code, even if views.py was deleted.
Please, provide me some suggestions/tips - what might cause this server behavior?
I had this problem. In my case, running the following command solved it :
touch myproject/wsgi.py
No need to restart server or delete .pyc files. It's all explained in mod_wsgi docs.

Is apache2 reload for .conf changes only or is it allowable to be used when application code changes?

When the code for my python WSGI applicaiton changes should I use apache2's reload or graceful restart feature?
Currently we use reload, but have noticed that sometimes the application does not load properly and errors pertaining to missing modules are logged to the error files even though the modules have existed for a long time.
If you can, you should probably use graceful. But if your application is not exiting correctly you may have to force it with just restart.
For wsgi, you should try running in daemon mode. When it is running in daemon mode, you can restart your service just by touching the wsgi file and updating its timestamp. This will reload all the code without restarting apache.
Here is more info: http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess
This is for django, but may be useful for your project: http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
The 'reload' and 'graceful' would have the same effect as far as reloading your web application. If you are seeing issues with imports like you describe, it is likely to be an issue in your application code with you having import order dependencies or import cycles. One sees this a lot with people using Django. Suggest you actually post an example of the error you are getting.

Restarting a Django application running on Apache + mod_python

I'm running a Django app on Apache + mod_python. When I make some changes to the code, sometimes they have effect immediately, other times they don't, until I restart Apache. However I don't really want to do that since it's a production server running other stuff too. Is there some other way to force that?
Just to make it clear, since I see some people get it wrong, I'm talking about a production environment. For development I'm using Django's development server, of course.
If possible, you should switch to mod_wsgi. This is now the recommended way to serve Django anyway, and is much more efficient in terms of memory and server resources.
In mod_wsgi, each site has a .wsgi file associated with it. To restart a site, just touch the relevant file, and only that code will be reloaded.
As others have suggested, use mod_wsgi instead. To get the ability for automatic reloading, through touching the WSGI script file, or through a monitor that looks for code changes, you must be using daemon mode on UNIX. A slight of hand can be used to achieve same on Windows when using embedded mode. All the details can be found in:
http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode
You can reduce number of connections to 1 by setting "MaxRequestsPerChild 1" in your httpd.conf file. But do it only on test server, not production.
or
If you don't want to kill existing connections and still restart apache you can restart it "gracefully" by performing "apache2ctl gracefully" - all existing connections will be allowed to complete.
Use a test server included in Django. (like ./manage.py runserver 0.0.0.0:8080) It will do most things you would need during development. The only drawback is that it cannot handle simultaneous requests with multi-threading.
I've heard that there is a trick that setting Apache's max instances to 1 so that every code change is reflected immediately--but because you said you're running other services, so this may not be your case.

Categories

Resources