MemoryError with Django while serving a movie file - django

In my Django application, I list the contents of a directory which contains movies (of around 400 MB). When I try to play the movie in the browser, I get MemoryError. I have this movie content inside the "media" folder which I have marked to serve as statically.
I believe this movie should have been served directly through my web server without passing the request to Django. Is there some error in my configuration or is there whole together a different solution available for serving movies as in my case.
I am using lighttpd with Django and FCGI.
Thanks.

You are running out of memory because you read the whole file in memory & buffer it before serving it. Remove the static url config from django urls.py and configure that url to be served by lighthttpd.
But the best way for movies of that size are best served is streaming. Take a look at any media streaming server and see if it helps you. This may help you.
Streaming movies by flowplayer and lighthttpd
--Sai

Could it be that you haven't configured lighttpd to handle requests to /media/ itself and Django is running in debug mode (DEBUG = True in your settings.py).
If you follow Django's own docs for lighttpd deployment, this shouldn't happen.

I solved the error myself.
Actually the problem was with a misconfiguration with my lighttpd server. The problem was that I had configured my webserver to redirect every request to Django and allow Django to process the request and server the response through the webserver.
So, what was happening is when I request to play a large movie file (say around 400 MB), this request went to Django and somehow Django was loading the file in the memory.
Since it was an embedded device with a limited memory, Django threw an "MemoryError".
I changed the configuration of my webserver and everything worked like a charm.
Hope this helps someone in the future. Cheers!

Related

Serve multiple Django applications from one server

Good morning. I have a dedicated ubuntu server behind my company's firewall. It is using Django, Gunicorn, and Nginx to serve an Intranet application to employees. The original app responds to the URL [server_name]/[original_application_name]. I want to serve additional apps from this server. I have followed this tutorial as I did when setting up the original app.
I can run Gunicorn and serve the app, I have created a second systemd service file that appears steady (copied from the original app with paths changed - service runs), same for a new 'sites-available' file in Nginx (copied from original and modified), new .sock file exists, binding appears successful. However, I have yet to hit on the right configuration combination between settings.py [allowed_hosts], [new_app].service, and nginx etc.
The original app is running and when I try a URL related to the new app it gives an error saying it cannot find the request in the url.py of the original app. The new app would be used by the IT dept. Ideally, the new URL would be something like: it.[server_name]/[new_application_name].
I have looked through other cases with this problem but most use Apache or are on a public hosting site. I have seen this but it requires a "socket file". My original app is not using a socket file. I was hoping to do this without interfering with the original app. Is a "socket file" required? How can I configure this to serve both apps? Never having done this, what will the new URL be? The URL for the admin site in both apps is 'admin/', how can I accommodate this? Thanks!
I combined into one file as you suggested and I am almost there! I have original site responding at [server_name]/inventory and new site responding at [server_name]/assets. Great! My only problem is the admin page. In both apps the admin site is called admin! So, [server_name]/admin brings up the original site. How can I get to the new admin page?

Uploading Python application for the first time. URLconfig URLS not working

It is my first time trying to get a Django application live on the net but I am having an issue with the URls.
The working local URL of my application is
http://localhost:8000/surveythree/ - This works as expected.
However when I upload my project to my hosting account I cant seem to locate the relevant page using the shortened URL as provided by the URLconf, in this case it should be /surveythree/
url(r'^surveythree/$', SurveyWizard.as_view([SurveyForm1, SurveyForm2, SurveyForm3, SurveyForm4, SurveyForm5])),
I can locate the page if I use the full filepath however.
http://www.mywebsite.com/bias_experiment/src/survey/templates/formtools/wizard/wizard_form.html
I thought the benefit of the URLconf was to shorten the URL to something like one of the following
http://www.mywebsite.com/bias_experiment/surveythree/
http://www.mywebsite.com/bias_experiment/src/surveythree/
http://www.mywebsite.com/bias_experiment/src/survey/surveythree/
Is there something simple that I am missing here? If anyone could tell me what the shortened URL should be based on the above it would be great. I have been trying multiple combinations for a while now but I don't know if I am going around in circles or doing it wrong.
Thanks in advance.
It doesn't look like you have actually deployed your site with a proper server. You can't just upload the files to any webserver and expect then to run: you need to configure a wsgi server and connect it to your app.
The documentation is here but to be honest I'd be amazed if your college server supported it at all, if all you have is a shared folder. You may be able to get it to work with FastCGI, but I wouldn't hold out a whole lot of hope.
(And even though you say that going to that long URL "works", I'd guarantee that all you're seeing is the raw HTML template. There's no way that any actual dynamic functionality will be working like that, as you'd see if you actually tried to submit the form at that URL.)
Deploying Django apps is much more complicated. To run in more "production" environment you will need to configure:
virtualenv to keep pip modules which your app required separate from global environment.
nginx for hosting static files ( you can copy them to some folder with ./manage.py collectstatic.
WSGI server: uWSGI or Gunicorn are both nice choices.
supervisor: for running and restarting WSGI and any other apps (for example celery) running in background
It's a lot for a start, so it's good to follow some tutorial and use ready to use config snippets.

Django serve files outside the web root

I currently have Django set up to upload files to:
/path/to/project/uploads
This works great. This folder is in the root folder of the project so the files cannot be served directly from a web URL, which is what I want, the files are "CVs" uploaded by users.
I've had a look at a third-party django app called filetransfers which would do the job, but I'm wondering if there is a way with Django core to serve files from outside the media folder.
Any help would be great.
Andy
Depending on what web server you are using I would recommend using X-sendfile if you use Apache or X-accel-redirect if you use Nginx. But remember you will need to change setting in your web server. But this is far more efficient way of serving files than using Django to do it.
If what you want is to keep control on how your files are served / who can see them etc, then the simplest solution is to write a custom view serving theses files. You just have to provide the file's content as the response body and set the appropriate response headers (file type, content length etc). Reading the FineManual(tm) part about the Response object should be a good starting point.
Resolved using FileWrapper().
Thanks anyway.

Django asks for session in every request (even image/css/etc). How to handle this?

I installed the devserver package, and i can see in sql log that for a requests, django asks for session multiple times, for every static media. How to stop this ?
Stop serving your media from django...
Use nginx or another webserver to handle media requests.

libjpeg PIL photologue django

So I have been looking up and down for a good solution to display images, audio, media in general with django:
From what I found there is the following solutions:
1. Photologue:
Cant seem to make it work.
It needs PIL and libjepg.
I tried to install both, but ran into different build problems.
Someone on stackoverflow recommended me this link:
http://appelfreelance.com/2010/06/libjpeg-pil-snow-leopard-python2-6-_jpeg_resync_to_restart/
Which is a great tutorial and also deals with removing PIL and reinstalling (which I have to do, I guess).
But somewhere along this tutorial I have to do this:
sudo rm -Rf build
And I dont feel confident enough to run a sudo rm command.
Also I thought there must be an easier way to achieve this, since EVERY Blog needs to display media.
2. Django itself says:
Let Apache do the display and use this:
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': '/pathtomedia/', 'show_indexes': True}),
for development BUT not for production.
So what does everybody use for production?
3. Other:
I managed to display images via os.listdir , urllist and a template:
allimg= os.listdir("/pathtomyimages/")
urllist = ['/site_media/Imagedir/%s' % url for url in allimg]
return render_to_response('picture_display.html',
{'allimg': allimg})
But this also seems not very promising, since I want to display images, audio, media from one directory on multiple sites.
I hear paginator could be the solution.
So now my question:
What is the best, easiest and robust solution to display media in django in development and in production?
What are you using?
Please help me out!
Thanks for the time.
django-photologue is an app for managing images and image collections for your project but isn't directly related to serving static media.
Since "best", "easiest", and "robust" are all fairly subjective terms depending on your specific needs, I recommend you first seek to understand why it is not common practice to serve your media files from Django. The short, generic answer is that Django must be loaded into each web server process that handles a request and this will use a non-trivial amount of memory. When serving a media file you (usually) just return a file from disk. Since no application logic is required you are incurring unnecessary overhead both from the memory required to run Django in a process and the extra code processing that will occur for Django to map the request path back to a view, load the file, and dump it into the response. Web servers (Apache, Nginx, etc.) are already very good at returning files from disk. This is why it is recommended that you have your web server handle static media files. The request does not ever need to get handed off to the WSGI middleware or Django.
There are many options available but there are a few that are likely to be common solutions. For example, if you've decided on Apache as your web server, you would need to configure it so that requests to your media path are handled directly by Apache and all other requests are handed off to Django. Apache + mod_wsgi is a fairly common choice AFAICT. Read through this carefully:
http://docs.djangoproject.com/en/1.2/howto/deployment/modwsgi/#howto-deployment-modwsgi
It is not strictly necessary to configure a separate web server as the documents recommend. You can use the same Apache instance for Django and media as you're getting started.
That tutorial is actually mine :)
the sudo RM -rf build, all this does is actually force remove any previous builds, which basically means it clears anything to do with the builds.
hope this helps