python - 如何停止htmlpy ui阻止

标签 python youtube backend

我当时正在使用Youtube(用于异步),htmlpy和pafy(和youtube-dl)来开发trollius视频的小型下载器。现在我有一个问题,一旦我单击下载按钮,UI就会冻结,而backend正在下载视频。

我试图使downloader类成为自己的线程,并与UI一起运行,但这似乎不起作用。我还尝试使用coroutineUI继续的同时异步下载视频。似乎都不起作用。

这是一些代码

class Downloader(htmlPy.Object,threading.Thread):
dd = os.path.join(os.getenv('USERPROFILE'), 'Downloads')  # dd = download director
dd = dd + "/vindownload/"

def __init__(self, app):
    threading.Thread.__init__(self)
    super(Downloader, self).__init__()
    # Initialize the class here, if required.
    self.app = app
    return

@htmlPy.Slot(str)
def download_single(self, json_data):
    form_data = json.loads(json_data)
    print json_data
    url = form_data["name"]
    dt = form_data["dt"]  # Download type is audio or video
    if url.__contains__("https://www.youtube.com/watch?v="):
        if dt == 'audio':
            print "hello1"
            loop = trollius.get_event_loop()
            loop.run_until_complete(self._downloadVid(url, vid=False))
            loop.stop()
        else:
            print "hello1"
            loop = trollius.get_event_loop()
            loop.run_until_complete(self._downloadVid(url, vid=True))
            loop.stop()
        self.app.evaluate_javascript("document.getElementById('form').reset()")
    else:
        print "Incorrect url"
    print form_data

 @trollius.coroutine
def _downloadVid(self, url, vid=False, order_reverse=False, vinName=None):
    print "hello123"
    video = pafy.new(url)
    print video
    name = u''.join(video.title).encode('utf8')
    name = re.sub("[<>:\"/\\|?*]", "", name)
    if not vid:
        file = video.getbestaudio()
    else:
        file = video.getbest()
    if (order_reverse):
        file.download(self.dd + vinName + name + ".mp4", quiet=False,callback=self.mycb)
    else:
        file.download(self.dd + name + ".mp4", quiet=False,callback=self.mycb)

def mycb(self,total, recvd, ratio, rate, eta):
    print(recvd, ratio, eta)

和我的initialize.py
BASE_DIR = os.path.abspath(os.path.dirname("initilize.py"))

app = htmlPy.AppGUI(title=u"Vin download", width=700, height=400, resizable=False)

app.static_path = os.path.join(BASE_DIR, "static/")
app.template_path = os.path.join(BASE_DIR, "templates/")

app.web_app.setMaximumWidth(830)
app.web_app.setMaximumHeight(600)

download  = Downloader(app)
download.start()

# Register back-end functionalities
app.bind(download)
app.template = ("./index.html", {"template_variable_name": "value"})

# Instructions for running application
if __name__ == "__main__":
    # The driver file will have to be imported everywhere in back-end.
    # So, always keep app.start() in if __name__ == "__main__" conditional
    app.start()

现在我的问题是。有没有一种方法可以让我在下载时释放UI,因此它看起来不像应用程序崩溃了。

我正在使用:Python 2.7,Trollius,Pafy,Youtube-dl,HTMLPY。

感谢您的时间。

最佳答案

好了,我找到了问题的答案,这就是我所做的。我将我的download_single方法更改为以下内容:

@htmlPy.Slot(str)
def download_single(self, json_data):
    form_data = json.loads(json_data)
    print json_data
    url = form_data["name"]
    dt = form_data["dt"]  # Download type is audio or video
    videoDown = videoDownload(url, dt, self.app, self.dd)
    videoDown.start()
    print form_data

现在,我之前使用的代码现在被转移到一个名为videoDownload的新类中,该类具有上述所有属性。

videoDownload.py
class videoDownload(threading.Thread):
def __init__(self, url, dt, app, dd):
    threading.Thread.__init__(self)
    self.url = url
    self.dt = dt
    self.app = app
    self.dd = dd

def run(self):
    threads.append(self)
    if self.url.__contains__("https://www.youtube.com/watch?v="):
        if self.dt == 'audio':
            print "hello1"
            self._downloadVid(self.url, vid=False)
        else:
            print "hello1"
            self._downloadVid(self.url, vid=True)
    else:
        print "Incorrect url"

def _downloadVid(self, url, vid=False, order_reverse=False, vinName=None):
    print "hello123"
    video = pafy.new(url)
    print video
    name = u''.join(video.title).encode('utf8')
    name = re.sub("[<>:\"/\\|?*]", "", name)
    if not vid:
        file = video.getbestaudio()
    else:
        file = video.getbest()
    if (order_reverse):
        file.download(self.dd + vinName + name + ".mp4", quiet=False, callback=self.mycb)
    else:
        file.download(self.dd + name + ".mp4", quiet=False, callback=self.mycb)
    threads.remove(self)

def mycb(self, total, recvd, ratio, rate, eta):
    pass

这解决了我在ui被阻止时遇到的问题。一旦离开run方法,线程将结束自身,并将其从类上方定义的线程数组中删除。

祝你有美好的一天

〜埃利斯山

关于python - 如何停止htmlpy ui阻止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990406/

相关文章:

architecture - 将数据访问层作为与服务层分离的层是否好?

python - Mechanize 中的 HTTP 登录

python - YouTube API Python upload_video.py从脚本而不是命令行运行

php - 从JSON问题中提取数据

javascript - 无法通过 Google Chrome 扩展中的 YouTube JS API 访问 YouTube 视频,但可以在 JS Fiddle 中访问

php - 更改 WooCommerce 管理产品页面设置中的 "Regular price"文本

javascript - Parse.Cloud.run() 未履行 promise

python - Pandas groupby - 对每组中的一半记录应用不同的函数

python - 如何编写 Python 包/模块?

Python 模拟、django 和请求