在python gtk中创建一个新的子进程
我正在创建一个 python gtk 程序,它将视频上传到 ubuntu 10.10 中的 youtube。它使用 Googlecl 包工作。您可以通过谷歌搜索了解更多信息。我已经通过以下语法实现了调用程序。
os.system('google youtube post --category %s --title \'%s\' --summary \'%s\' --tags %s %s' % ("Education",title,description,tags,filename))
现在一切正常,视频已正确上传到 youtube。然而,根据视频大小,上传需要大量时间,当然也取决于网络速度。上传时,我的 python gtk 程序变成灰色(冻结,在 ubuntu 中无响应),只有在视频上传后,用户才能再次与程序交互。
有没有办法将上传部分分离到子进程,以便用户在另一个后台进程上传视频时仍然可以与程序交互?我喜欢显示一个进度条窗口,它会向用户表明程序仍在运行并正常工作。
为此,我创建了一个 Progress_bar 窗口并输入以下代码。
progress_bar.show()
os.system('google youtube post --category %s --title \'%s\' --summary \'%s\' --tags %s %s' % ("Education",title,description,tags,filename))
progressbar.set_fraction(0.5)
但是,当我执行程序时,进度条窗口不显示,并且视频正在上传,这使得我的程序在上传完成之前没有响应。
更新:有没有办法做到这一点类似于进度条不更新。这样我只需要在代码中做一个小的改变,但是我不知道该方法中声明的 while 循环 while Heavy_work_needed
中的条件是什么。
I am creating a python gtk program which uploads videos to youtube in ubuntu 10.10. It works using the Googlecl package. You can google it for more info. I've implemented calling the program through the following syntax.
os.system('google youtube post --category %s --title \'%s\' --summary \'%s\' --tags %s %s' % ("Education",title,description,tags,filename))
Now it works fine, that is the video is uploaded properly to youtube. However depending on the video size it takes a lot of time to upload depending ofcourse on the network speed. While it is uploading, my python gtk program turns gray (freezes, unresponsive in ubuntu) and only after the video is uploaded can the user interact with the program again.
Is there a way to seperate the uploading part to a subprocess such that the user can still interact with the program while the video is being uploaded in some another background process? I like to show a progress bar window which will indicate to the user that the program is still running and working properly.
To do this, I created a progress_bar window and entered the following code.
progress_bar.show()
os.system('google youtube post --category %s --title \'%s\' --summary \'%s\' --tags %s %s' % ("Education",title,description,tags,filename))
progressbar.set_fraction(0.5)
However when I execute the program, the progress bar window does not show and the video is being uploaded which makes my program unresponsive until the uploading is complete.
Update: Is there a way to do this similar to progress bar not updating. In this way I need to only make a small change in my code, however I do not know what the condition is in the while loop while heavy_work_needed
stated in that method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下使用 os.system 将不起作用:运行 os.system 时,它将等到您启动的进程完成后再继续执行代码。
在这种情况下,直到视频上传后才会调用 set_fraction 方法。进度条也不会显示,因为您的应用程序在上传时“卡在”os.system 中,因此永远不会将控制权交还给 Gtk 来处理绘图等事件。
尝试查看 subprocess 模块。这将允许您在后台运行上传过程并知道它何时完成。
Using os.system in this case won't work: when running os.system it'll wait till the process you started finished before continuing with your code.
In this case the set_fraction method will never be called till after the video is uploaded. The progressbar is also not shown as your app is "stuck" in os.system while uploading, and so never gives control back to Gtk to handle events like drawing.
Try looking at the subprocess module. This will allow you to run the upload process in the background and know when it finishes.
您可以在单独的线程中执行耗时的操作。 PyGTK FAQ 有示例如何耦合线程和UI 更新(请注意,所有 UI 更新都应在 Gtk+ 线程中完成)。
You could do time-consuming operation in a separate thread. PyGTK FAQ has example how to couple threading and UI updates (mind that all UI updates should be done in Gtk+ thread).