Instantiate object "at module level" in Django
Question: I want to use an [mpd client](https://github.com/Mic92/python-mpd2) in a
Django project and would like to avoid having more connections to the mpd
server than necessary. I think the easiest way to achieve this is to reuse the
mpd-client object instead of creating a new object for every request.

In short, I'd like to do something very similar to this: [Django: Keep a
persistent reference to an
object?](http://stackoverflow.com/questions/18130698/django-keep-a-persistent-
reference-to-an-object).

@daniel-roseman states this is easy to achieve by simply instantiating the
object at module level. However, as a python-newbie I don't quite understand
what that means.

So far I have created a module (see below) that would reconnect to mpd in case
of a disconnect and saved this module to `<Project>/<app>/lib/MPDProxy.py`.

How would I instantiate this (mpd-)object at module level?

    
    
    # MPDProxy.py
    from mpd import MPDClient, MPDError
    
    class MPDProxy:
        def __init__(self, host="localhost", port=6600, timeout=10):
            self.client = MPDClient()
            self.host = host
            self.port = port
    
            self.client.timeout = timeout
            self.connect(host, port)
    
        def connect(self, host, port):
            self.client.connect(host, port)
            self.client.consume(1) # when we call self.client.next() the previous stream is deleted from the playlist
            if len(self.client.playlist()) > 1:
                cur =  (self.client.playlist()[0][6:])
                self.client.clear()
                self.add(cur)
    
        def add(self, url):
            try:
                self.client.add(url)
            except ConnectionError:
                self.connect(self.host, self.port)
                self.client.add(url)
    
        def play(self):
            try:
                self.client.play()
            except ConnectionError:
                self.connect(self.host, self.port)
                self.client.play()
    
        def stop(self):
            try:
                self.client.stop()
            except ConnectionError:
                self.connect(self.host, self.port)
                self.client.stop()
    
        def next(self):
            try:
                self.client.next()
            except ConnectionError:
                self.connect(self.host, self.port)
                self.client.next()
    
        def current_song(self):
            try:
                return self.client.currentsong()
            except ConnectionError:
                self.connect(self.host, self.port)
                return self.client.current_song()
    
        def add_and_play(self, url):
            self.add(url)
    
            if self.client.status()['state'] != "play":
                self.play()
    
            self.next()
    


Answer: I just mean the bottom level of that module, at the same indentation as the
"from mpd..." and "class MPDProxy..." lines.

So, at the bottom of the file, without indenting at all, put `proxy =
MPDProxy()` \- and now you can reference that instance from anywhere by
importing it with `from lib.MPDProxy import proxy`.

