python - wxPython 与长时间运行的进程卡住动画 gif

标签 python multithreading wxpython animated-gif

我正在尝试运行 python 长进程,但我需要对未卡住显示动画 gif 的用户说。

我的问题是我有 3 或 4 个长进程,要运行第二个进程,它取决于第一个进程的结果,因此这使得不可能在线程中运行这些进程。 有什么方法可以做到这一点吗? 在我的示例中,我只有两个过程,只是为了说明我这样做的方式。

import wx
from wx.adv import Animation, AnimationCtrl
import speedtest
import socket

class MainFrame(wx.Frame):
    def __init__(self, *args, **kw):
        # ensure the parent's __init__ is called
        super(MainFrame, self).__init__(*args, **kw)

        # create a panel in the frame
        self.pnl = wx.Panel(self)
        self.pnl.SetBackgroundColour(wx.Colour(81, 87, 129))

        # and put some text with a larger bold font on it
        st = wx.StaticText(self.pnl, label="Speed Test", pos=(200,10))
        font = st.GetFont()
        font.PointSize = 14
        font = font.Bold()
        st.SetForegroundColour("white")
        st.SetFont(font)

        self.anim = Animation('pictures/wait.gif')
        self.ctrl = AnimationCtrl(self.pnl, -1, self.anim)
        self.ctrl.SetPosition((70,50))

    def AnimeControl(self, status):
        if status:
            self.ctrl.Play()
        else:
            self.ctrl.Stop()

class NetworkChecks():
    def __init__(self):
        pass

    def resolve_dns(self, host):
        try:
            data = socket.gethostbyname_ex(host)
            if "error" in data:
                return False
        except Exception, e:
            print "resolve_dns: ", e
            return False
        return data

    def speed_test(self):
        try:
            servers = []
            threads = None
            st = speedtest.Speedtest()
            st.get_servers(servers)
            st.get_best_server()
            st.download(threads=threads)
            st.upload(threads=threads)
            st.results.share()
            return st.results.dict()
        except Exception, e:
            print "speed_test: ", e
            return False 

if __name__ == '__main__':
    app = wx.App()
    frm = MainFrame(None, title='NDT', size=(480, 330), style=wx.DEFAULT_DIALOG_STYLE | wx.MINIMIZE_BOX)
    frm.AnimeControl(True)
    frm.Show()

    wx.Yield()

    dns_name = NetworkChecks().resolve_dns('dns.google')

    if dns_name != False:
        speed_results = NetworkChecks().speed_test()
        if speed_results != False:
            print "Speed test results:"
            print "Internet ip: " + speed_results['client']['ip']
            print "Service ip : " +  speed_results['client']['isp']
            print "Download tx: " + '{0:.4g}'.format(speed_results['download'] / 1000000) + " Mbps"
            print "Upload tx  : " + '{0:.4g}'.format(speed_results['upload'] / 1000000) + " Mbps"
        else:
            print "Was not possible to test the internet link speed "
    frm.AnimeControl(False)
    app.MainLoop()

最佳答案

您应该在单独的线程中启动长时间运行的进程,而不是在主 GUI 线程中。请参阅此处了解一些想法和实现:

https://wiki.wxpython.org/LongRunningTasks

使用 wx.CallAfter 和 wx.PostEvent 是在其他线程和进程继续执行其操作时保持 GUI 响应的最佳选择。

关于python - wxPython 与长时间运行的进程卡住动画 gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57597886/

相关文章:

python - 在 wx.python 中使顶部窗口不可访问

python - 无法理解为什么正常停止线程会在Windows下的(wx)Python中挂起该线程的其余执行代码

Python交互模式历史和方向键

python - 从列表中删除唯一值并仅保留重复项

python - 子进程在使用多处理时不考虑参数

multithreading - Mule 线程配置文件的 poolExhaustedAction - RUN

python - 如何使用 python 和 Matplotlib 更新绘图

python - CentOS下安装idle-python2.7

python - 为什么 urllib 从一些维基百科文章中返回垃圾?

android - 在后台工作时更新 UI