create a new subprocess in python gtk
I am creating a python gtk program which u开发者_如何转开发ploads 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.
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.
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).
精彩评论