操作过程中进度条不更新
在我的 python 程序中将文件上传到互联网,我使用 GTK 进度条来显示上传进度。 但我面临的问题是,进度条在上传完成之前不显示任何活动,然后突然指示上传完成。 我使用 pycurl 发出 http 请求...我的问题是 - 我是否需要一个多线程应用程序来上传文件并同时更新 GUI? 或者我犯了什么其他错误?
提前致谢!
in my python program to upload a file to the internet, im using a GTK progress bar to show the upload progress. But the problems that im facing is that the progress bar does not show any activity until the upload is complete, and then it abruptly indicates upload complete. im using pycurl to make the http requests...my question is -
do i need to have a multi-threaded application to upload the file and simultaneously update the gui? or is there some other mistake that im making?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我将引用 PyGTK 常见问题解答 :
I'm going to quote the PyGTK FAQ:
问题很可能是在你的进度回调中,我假设你正在更新进度条,你没有调用手动更新显示,即通过 GUI 的事件循环运行。 但这只是猜测,如果您可以提供更多代码,进一步缩小范围可能会更容易。
您需要手动更新显示的原因是因为您的主线程也在执行上传,这是它被阻塞的地方。
More than likely the issue is that in your progress callback, which is where I presume you're updating the progress bar, you're not making a call to manually update the display i.e. run through the GUI's event loop. This is just speculation though, if you can provide more code, it might be easier to narrow it down further.
The reason you need to manually update the display is because your main thread is also performing the upload, which is where it's blocking.
在 python 2.x 中,整数操作数导致整数除法。 尝试这个:
In python 2.x integer operands result in integer division. Try this:
是的,您可能需要并发性,并且线程是一种方法,但是如果您确实使用线程,请使用如下方法: http://unpythonic.blogspot.com/2007/08/using-threads-in-pygtk.html 这将消除痛苦,并且让您专注于重要的方面。
(由于懒惰,我没有重复该博客文章中的所有内容,因此是社区维基)。
Yes, you probably need concurrency, and yes threads are one approach, but if you do use threads, please use an method like this one: http://unpythonic.blogspot.com/2007/08/using-threads-in-pygtk.html which will abstract away the pain, and allow you to focus on the important aspects.
(I have not repeated everything in that blog post through laziness, hence community wiki).
如果您没有使用 pycurl,一种选择是使用 GObject 的 IO 观察程序。
http://pygtk.org/pygtk2reference/gobject -functions.html#function-gobject--io-add-watch
使用此功能,您可以将文件上传与正常的 PyGTK 事件循环交错,甚至可以在 IO 监视回调中执行 set_progress 调用。 如果您要卸载所有上传到 pycurl 的工作,这实际上不太可行,但如果您只是通过 HTTP 上传文件,io_add_watch 也将使使用套接字来减轻痛苦。
One option, if you are not married to pycurl, is to use GObject's IO watchers.
http://pygtk.org/pygtk2reference/gobject-functions.html#function-gobject--io-add-watch
Using this you can interleave the file upload with the normal PyGTK event loop, and even do the set_progress call in your IO watch callback. If you are offloading all the work for uploading onto pycurl this is not really feasible, but if you're just uploading a file over HTTP, io_add_watch will make using a socket for this much less painful as well.