从 youtube-dl 读取 eta 等参数 [英] Read parameters like eta from youtube-dl

查看:38
本文介绍了从 youtube-dl 读取 eta 等参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 cmd 上读取 youtube dl 的输出并放入我的 wxpython 程序.这是我使用的功能.

Hi I would like to read the output from youtube dl on cmd and put in my wxpython program. This is the function I used.

        def execute(self,command,textctrl):
                process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                output = ''

                # Poll process for new output until finished
                for line in iter(process.stdout.readline, ""):
                    textctrl.AppendText(line)
                    output += line


                process.wait()
                exitCode = process.returncode

                if (exitCode == 0):
                    return output
                else:
                    raise Exception(command, exitCode, output)

wxpython 程序开始更新 textctrl 然后死机.它没有更新eta、大小、速度等

The wxpython program started updating the textctrl and then froze. It didnt update the eta, size, speed etc

推荐答案

只要你在这个函数中被阻塞并且不让控制返回到事件循环,那么就不会有事件分派给处理程序.没有事件被发送和处理,就不能重新绘制小部件的内容,不能与鼠标和键盘交互,什么都没有.基本上,应用程序被冻结是因为您的执行函数没有让它心跳,并且大脑与身体的其他部分隔绝了.

As long as you are blocked in this function and are not letting control return to the event loop, then there can be no events dispatched to handlers. With no events being sent and processed, there can be no repainting of the contents of the widgets, no interaction with with the mouse and keyboard, nothing. Basically the application is frozen because your execute function is not letting its heart beat and the brain is cut off from the rest of the body.

在对 GUI 或事件驱动编程的其他实现进行编程时,关键是永远不要在事件处理程序或回调中执行任何在返回事件循环之前花费超过明显(人工)时间的任何事情.如果您有一些需要比该时间更长的时间,那么您需要重新设计它,以便以不同的方式管理长时间运行的任务.

When programming GUIs or other implementations of event driven programming the key is to never do anything in an event handler or callback that would take more than a noticeable (by a human) amount of time before it returns to the event loop. If you have something that will take longer than that time, then you need to redesign it so the long running task is managed a different way.

一种方法是在事件处理程序中进行设置(例如启动进程),然后从事件处理程序返回.该设置的一部分是启动一个定时器,它会定期返回并检查是否有可用的输出.如果是,则读取它(无阻塞)并处理它,然后再次返回到事件循环.继续直到处理完成,然后在处理完最后一块数据后停止计时器.

One way would be to set things up in the event handler (such as starting the process) and then return from the event handler. Part of that setup would be to start a timer that comes back periodically and checks if there is output available. If so then read it (without blocking) and process it, and then return to the event loop again. Continue until the process is done and then stop the timer after the last chunk of data is processed.

另一种方法是使用线程来运行长时间运行的任务.这是一种常见的方法,但您需要注意不要从工作线程操作任何 UI 对象.因此,在您的示例中,您从进程中读取的文本需要发送回 GUI 线程,以便将其附加到文本控件.wx.CallAfter 是一种简单的方法.

Another approach is to use a thread to run the long running task. This is a common approach but you need to be careful to not manipulate any UI objects from the worker thread. So in your example the text you read from the process will need to be sent back to the GUI thread in order to have it appended to the text control. wx.CallAfter is an easy way to do that.

有关更多详细信息和一些示例,请参阅 https://wiki.wxpython.org/LongRunningTasks.

See https://wiki.wxpython.org/LongRunningTasks for more details and some examples.

这篇关于从 youtube-dl 读取 eta 等参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆