Pygtk 线程化长命令行进程
我在 Windows 上使用 PyGtk。我的程序使用 GraphicsMagick 将 PDF 转换为图像,然后显示该图像。在多页 PDF 上,转换时间可能会很长,因此程序必须等待命令行返回,我需要解决这个问题,并允许用户在创建图像时使用 GUI。我尝试使用gtk的线程和python的线程到目前为止还没有成功,有没有一种方法可以轻松地将GraphicsMagick转换作为后台进程调用并返回而不等待它返回?
我正在使用 subprocess(command) 从命令行执行转换。
I am in PyGtk on Windows. My program converts PDF's to images using GraphicsMagick then displays the image. The conversion can be very long on multi-page PDF's so the program has to wait for the command line to return and I need to get around this and allow the user to use the GUI while the image is created. My attempts to use gtk's threading and python's threading have not worked so far, is there a way to easily call the GraphicsMagick conversion as a background process and returnot wait for it to return?
I am using subprocess(command) to perform the conversion from the command line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需使用 subprocess.Popen:
有关更多信息,请参阅:
http://docs.python.org/library/subprocess.html #using-the-subprocess-module
如果确实需要使用线程,请在
gtk.main()
之前使用gtk.gdk.threads_init()
http://library.gnome.org/devel /pygtk/stable/gdk-functions.html#function-gdk--threads-init
Just use subprocess.Popen:
For more information see:
http://docs.python.org/library/subprocess.html#using-the-subprocess-module
If you really need to use threads use
gtk.gdk.threads_init()
beforegtk.main()
http://library.gnome.org/devel/pygtk/stable/gdk-functions.html#function-gdk--threads-init
您可以使用一个空闲函数,该函数调用
subprocess.Popen()
并在仍有页面需要处理时返回True
。这意味着它将在下一个可用机会时再次调用。当您完成页面后,您可以从空闲函数返回False
,并且它将不再被调用。You could use an idle function that calls
subprocess.Popen()
and returnsTrue
if there are still pages left to process. That means it will be called again at the next available opportunity. When you are finished with the pages, you can returnFalse
from the idle function and it will not be called anymore.