Using array of URLs for MEDIA_URL

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Using array of URLs for MEDIA_URL

Martin Siniawski
Hey guys,

I wanted to ask if there's any way or accepted solution for using more
than one URL simultaneously as the MEDIA_URL, so as to enable browsers
to further parallelize the download of static content. That is, having
something like:

MEDIA_URL = [ 'static1.site.com', 'static2.site.com',
'static3.site.com' ]

I've been searching through the group, documentation and online but
couldn't find anything that addressed the subject.

Thanks,
Martin

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

fvox13
Hi Martin-

I don't know what the "official" answer would be, but you could write your own
context processor that had something like:

from random import choice
MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
def media(request):
    return {'MEDIA_URL': choice(MEDIA_URLS)}

Then, in settings.py, include your custom context processor instead of the one
built-in to to Django.

-Steve



====================================
Steven L Smith, Web Developer
Department of Information Technology Services
Nazareth College of Rochester
585-389-2085   |   [hidden email]
http://www.naz.edu/pub/~ssmith46
====================================

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

Martin Siniawski
Steve,

Thanks for the answer.

If I understand correctly your idea and code, each template would have
only one value for the MEDIA_URL (randomly chosen from the array),
right?

What I was looking for was a way of distributing the value of the
MEDIA_URL uniformly along the values of an array, in each template. So
in a certain template the MEDIA_URL would have more than one value.
Maybe if I set it as a callable (with the logic that choses the values
inside it) that would work.

I cannot quite understand why there isn't anymore people running into
this issue.

Best and thanks again for the answer,
Martin

On Jun 7, 10:46 am, Steven L Smith <[hidden email]> wrote:

> Hi Martin-
>
> I don't know what the "official" answer would be, but you could write your own
> context processor that had something like:
>
> from random import choice
> MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> def media(request):
>     return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> Then, in settings.py, include your custom context processor instead of the one
> built-in to to Django.
>
> -Steve
>
> ====================================
> Steven L Smith, Web Developer
> Department of Information Technology Services
> Nazareth College of Rochester
> 585-389-2085   |   [hidden email]://www.naz.edu/pub/~ssmith46
> ====================================

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

fvox13
In reply to this post by Martin Siniawski
Hi Martin-

Yes, that is what that code would do... I guess I forgot about how context
processors work. :-P

I think most people do this type of thing with a round-robin server setup. If
you wanted to do it django-side, you'd probably need to do a template tag.
Note the callable...

-Steve



random_media.py
-----------------------------
from django import template
from random import choice

register = template.Library()

def media_url():
   media_urls = ['site1.example.com', 'site2.example.com',
'site3.example.com']
   return choice(media_urls)
register.simple_tag(media_url)


in some template
------------------
{% load random_media %}
{% media_url %}





====================================
Steven L Smith, Web Developer
Department of Information Technology Services
Nazareth College of Rochester
585-389-2085   |   [hidden email]
http://www.naz.edu/pub/~ssmith46
====================================

----- Original Message -----
From: "Martin Siniawski" <[hidden email]>
To: "Django users" <[hidden email]>
Sent: Tuesday, June 8, 2010 8:32:53 AM GMT -05:00 US/Canada Eastern
Subject: Re: Using array of URLs for MEDIA_URL

Steve,

Thanks for the answer.

If I understand correctly your idea and code, each template would have
only one value for the MEDIA_URL (randomly chosen from the array),
right?

What I was looking for was a way of distributing the value of the
MEDIA_URL uniformly along the values of an array, in each template. So
in a certain template the MEDIA_URL would have more than one value.
Maybe if I set it as a callable (with the logic that choses the values
inside it) that would work.

I cannot quite understand why there isn't anymore people running into
this issue.

Best and thanks again for the answer,
Martin

On Jun 7, 10:46 am, Steven L Smith <[hidden email]> wrote:
> Hi Martin-
>
> I don't know what the "official" answer would be, but you could write your
own
> context processor that had something like:
>
> from random import choice
> MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> def media(request):
>     return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> Then, in settings.py, include your custom context processor instead of the
one

> built-in to to Django.
>
> -Steve
>
> ====================================
> Steven L Smith, Web Developer
> Department of Information Technology Services
> Nazareth College of Rochester
> 585-389-2085   |   [hidden email]://www.naz.edu/pub/~ssmith46
> ====================================

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to
[hidden email].
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

fvox13
In reply to this post by Martin Siniawski
Looks like my email client screwed up the indentation.... I've added some
indents...

random_media.py
-----------------------------
from django import template
from random import choice

register = template.Library()

def media_url():
    media_urls = ['site1.example.com', 'site2.example.com',
'site3.example.com']
    return choice(media_urls)
register.simple_tag(media_url)


in some template
------------------
{% load random_media %}
{% media_url %}





====================================
Steven L Smith, Web Developer
Department of Information Technology Services
Nazareth College of Rochester
585-389-2085 begin_of_the_skype_highlighting              585-389-2085      
end_of_the_skype_highlighting | [hidden email]
http://www.naz.edu/pub/~ssmith46
====================================

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

Alex Robbins
In reply to this post by Martin Siniawski
I haven't tested this, but it seems like you could do it one of two
ways:

Define media root as a function in your settings.py. If you pass a
callable to the template, it will be called.
MEDIA_ROOTS = ['server1', 'server2', 'server3']
def MEDIA_ROOT():
    from random import choice
    return choice(MEDIA_ROOTS)

OR

Use a context processor to add MEDIA_ROOTS to the context and use the
random template tag.

{{ MEDIA_ROOTS|random }}


Steven's custom template tag works too.

Alex
On Jun 8, 7:32 am, Martin Siniawski <[hidden email]> wrote:

> Steve,
>
> Thanks for the answer.
>
> If I understand correctly your idea and code, each template would have
> only one value for the MEDIA_URL (randomly chosen from the array),
> right?
>
> What I was looking for was a way of distributing the value of the
> MEDIA_URL uniformly along the values of an array, in each template. So
> in a certain template the MEDIA_URL would have more than one value.
> Maybe if I set it as a callable (with the logic that choses the values
> inside it) that would work.
>
> I cannot quite understand why there isn't anymore people running into
> this issue.
>
> Best and thanks again for the answer,
> Martin
>
> On Jun 7, 10:46 am, Steven L Smith <[hidden email]> wrote:
>
> > Hi Martin-
>
> > I don't know what the "official" answer would be, but you could write your own
> > context processor that had something like:
>
> > from random import choice
> > MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> > def media(request):
> >     return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> > Then, in settings.py, include your custom context processor instead of the one
> > built-in to to Django.
>
> > -Steve
>
> > ====================================
> > Steven L Smith, Web Developer
> > Department of Information Technology Services
> > Nazareth College of Rochester
> > 585-389-2085   |   [hidden email]://www.naz.edu/pub/~ssmith46
> > ====================================

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

Martin Siniawski
In reply to this post by fvox13
Steven,

Thanks again for the answer.

I couldn't quite understand the comment regarding the round robin
server setup. In order to workaround the browser's limitations to
simultaneous connections that can be opened to a host, the links to
static content in the HTML content must be to different hosts. How
would that be done with the round robin server setup you mentioned?

Thanks for the proposed solution as well. It seems to be a good
solution, but it would imply changing all the templates, and I don't
even know what would happen to those templates that are from external
apps.

I'll post a solution that I coded on my own, though I haven't yet
benchmarked it.

Best,
Martin

On Jun 8, 11:01 am, Steven L Smith <[hidden email]> wrote:

> Looks like my email client screwed up the indentation.... I've added some
> indents...
>
> random_media.py
> -----------------------------
> from django import template
> from random import choice
>
> register = template.Library()
>
> defmedia_url():
>     media_urls = ['site1.example.com', 'site2.example.com',
> 'site3.example.com']
>     return choice(media_urls)
> register.simple_tag(media_url)
>
> in some template
> ------------------
> {% load random_media %}
> {%media_url%}
>
> ====================================
> Steven L Smith, Web Developer
> Department of Information Technology Services
> Nazareth College of Rochester
> 585-389-2085 begin_of_the_skype_highlighting              585-389-2085      
> end_of_the_skype_highlighting | [hidden email]://www.naz.edu/pub/~ssmith46
> ====================================

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

Martin Siniawski
Steven and Alex,

This is what I coded, and tried it out and works. I still have to
benchmark it.

from django.conf import settings

class RoundRobinMediaUrl():
    current_url = 0
    try:
        media_urls = settings.MEDIA_URLS
    except:
        media_urls = (settings.MEDIA_URL,)

    def __unicode__(self):
        self.current_url += 1
        self.current_url %= len(self.media_urls)
        return self.media_urls[self.current_url]

def context_proc (req):
    return {'MEDIA_URL': RoundRobinMediaUrl()}

On Jun 11, 3:09 pm, Martin Siniawski <[hidden email]> wrote:

> Steven,
>
> Thanks again for the answer.
>
> I couldn't quite understand the comment regarding the round robin
> server setup. In order to workaround the browser's limitations to
> simultaneous connections that can be opened to a host, the links to
> static content in the HTML content must be to different hosts. How
> would that be done with the round robin server setup you mentioned?
>
> Thanks for the proposed solution as well. It seems to be a good
> solution, but it would imply changing all the templates, and I don't
> even know what would happen to those templates that are from external
> apps.
>
> I'll post a solution that I coded on my own, though I haven't yet
> benchmarked it.
>
> Best,
> Martin
>
> On Jun 8, 11:01 am, Steven L Smith <[hidden email]> wrote:
>
>
>
> > Looks like my email client screwed up the indentation.... I've added some
> > indents...
>
> > random_media.py
> > -----------------------------
> > from django import template
> > from random import choice
>
> > register = template.Library()
>
> > defmedia_url():
> >     media_urls = ['site1.example.com', 'site2.example.com',
> > 'site3.example.com']
> >     return choice(media_urls)
> > register.simple_tag(media_url)
>
> > in some template
> > ------------------
> > {% load random_media %}
> > {%media_url%}
>
> > ====================================
> > Steven L Smith, Web Developer
> > Department of Information Technology Services
> > Nazareth College of Rochester
> > 585-389-2085 begin_of_the_skype_highlighting              585-389-2085      
> > end_of_the_skype_highlighting | [hidden email]://www.naz.edu/pub/~ssmith46
> > ====================================

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|

Re: Using array of URLs for MEDIA_URL

Martin Siniawski
In reply to this post by Alex Robbins
Alex,

Thanks for the answer.

I tried doing something like the first thing you proposed (MEDIA_URL
as a callable) but I had to tweak it a little bit because the template
wasn't calling the callable, but rather printing 'function
object .....'.

What do you think of the solution I posted?

Best,
Martin

On Jun 8, 11:29 am, Alex Robbins <[hidden email]>
wrote:

> I haven't tested this, but it seems like you could do it one of two
> ways:
>
> Define media root as a function in your settings.py. If you pass a
> callable to the template, it will be called.
> MEDIA_ROOTS = ['server1', 'server2', 'server3']
> def MEDIA_ROOT():
>     from random import choice
>     return choice(MEDIA_ROOTS)
>
> OR
>
> Use a context processor to add MEDIA_ROOTS to the context and use the
> random template tag.
>
> {{ MEDIA_ROOTS|random }}
>
> Steven's custom template tag works too.
>
> Alex
> On Jun 8, 7:32 am, Martin Siniawski <[hidden email]> wrote:
>
>
>
> > Steve,
>
> > Thanks for the answer.
>
> > If I understand correctly your idea and code, each template would have
> > only one value for the MEDIA_URL (randomly chosen from the array),
> > right?
>
> > What I was looking for was a way of distributing the value of the
> > MEDIA_URL uniformly along the values of an array, in each template. So
> > in a certain template the MEDIA_URL would have more than one value.
> > Maybe if I set it as a callable (with the logic that choses the values
> > inside it) that would work.
>
> > I cannot quite understand why there isn't anymore people running into
> > this issue.
>
> > Best and thanks again for the answer,
> > Martin
>
> > On Jun 7, 10:46 am, Steven L Smith <[hidden email]> wrote:
>
> > > Hi Martin-
>
> > > I don't know what the "official" answer would be, but you could write your own
> > > context processor that had something like:
>
> > > from random import choice
> > > MEDIA_URLS =  'static1.site.com', 'static2.site.com', 'static3.site.com' ]
> > > def media(request):
> > >     return {'MEDIA_URL': choice(MEDIA_URLS)}
>
> > > Then, in settings.py, include your custom context processor instead of the one
> > > built-in to to Django.
>
> > > -Steve
>
> > > ====================================
> > > Steven L Smith, Web Developer
> > > Department of Information Technology Services
> > > Nazareth College of Rochester
> > > 585-389-2085   |   [hidden email]://www.naz.edu/pub/~ssmith46
> > > ====================================

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.