Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 实例化对象";“在模块级”;在Django_Python_Django_Python 3.x - Fatal编程技术网

Python 实例化对象";“在模块级”;在Django

Python 实例化对象";“在模块级”;在Django,python,django,python-3.x,Python,Django,Python 3.x,我希望在Django项目中使用,并且希望避免与mpd服务器的连接过多。我认为实现这一点最简单的方法是重用mpd客户机对象,而不是为每个请求创建一个新对象 简而言之,我想做一些非常类似的事情: @daniel roseman表示,通过简单地在模块级实例化对象,很容易实现这一点。然而,作为一名python新手,我不太明白这意味着什么 到目前为止,我已经创建了一个模块(见下文),在断开连接时将重新连接到mpd,并将该模块保存到//lib/MPDProxy.py 如何在模块级实例化这个(mpd-)对象

我希望在Django项目中使用,并且希望避免与mpd服务器的连接过多。我认为实现这一点最简单的方法是重用mpd客户机对象,而不是为每个请求创建一个新对象

简而言之,我想做一些非常类似的事情:

@daniel roseman表示,通过简单地在模块级实例化对象,很容易实现这一点。然而,作为一名python新手,我不太明白这意味着什么

到目前为止,我已经创建了一个模块(见下文),在断开连接时将重新连接到mpd,并将该模块保存到
//lib/MPDProxy.py

如何在模块级实例化这个(mpd-)对象

# 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()

我只是指模块的底层,与“from mpd…”和“class MPDProxy…”行的缩进相同


因此,在文件的底部,完全不缩进,放置
proxy=MPDProxy()
——现在您可以通过从lib.MPDProxy import proxy导入
从任何地方引用该实例,谢谢!这是有道理的!只有一件事:它不应该是来自.lib.MPDProxy导入代理的
?如果没有领先的
,它将无法在我的环境中工作。