如何从同一资源并发中绘制多个图

时间:2019-06-27 07:53:43

标签: python plot concurrency wxpython

我有2个绘图按钮,它们从同一资源中获取不同的数据。处理大约需要5秒钟。为了能够这次按下第二个按钮,我使用的是 wx.lib.delayedresult

wx.lib.delayedresult 之前,按第一个按钮--->处理需要5秒钟(在此期间,我无法按第二个按钮)--->结果1。--- >按下第二个按钮---> 5s处理--->结果2。因此,所有过程需要10-11秒。

wx.lib.delayedresult 之后,按第一个按钮--->处理需要5s(在此期间,按第二个按钮)--->结果1 ---> 5s --- >结果2.因此,所有过程需要10到11秒。

我想要的是,按第一个按钮---> 5s处理(这次也按第二个按钮)--->结果1和结果2。所有操作都需要5-6秒。

只有通过 wx.lib.delayedresult 才有可能吗?还是我应该使用线程,队列,处理等?

任何建议将不胜感激。

这是wx.lib.delayedresult的一个小例子。

from time import sleep
import wx
from wx.lib.delayedresult import startWorker    

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.startButton = wx.Button(self.panel, label="Long Task")
        self.Button1 = wx.Button(self.panel, label="btn1")
        self.Button2 = wx.Button(self.panel, label="btn2")

        self.startButton.Bind(wx.EVT_BUTTON, self.OnStartButton)
        self.Button1.Bind(wx.EVT_BUTTON, self.OnBtn)
        self.Button2.Bind(wx.EVT_BUTTON, self.heyBtn)

        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.startButton)
        self.sizer.Add(self.Button1)
        self.sizer.Add(self.Button2)
        self.sizer.Add((10, 10))

        self.border = wx.BoxSizer()
        self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)

        self.panel.SetSizerAndFit(self.border)  
        self.SetSizerAndFit(self.windowSizer)   
        self.Show()

    def OnStartButton(self, e):
        self.startButton.Disable()
        startWorker(self.LongTaskDone, self.LongTask)

    def OnBtn(self, e):
        hey(self).Show()

    def heyBtn(self, event):
        self.Button2.Disable()
        startWorker(self.LongTaskDone_2, self.LongTask)

    def LongTask_2(self):
        print("LT 2")
        sleep(5)

    def LongTaskDone_2(self, result):
        self.Button2.Enable()
        print("ended 2")

    def LongTask(self):
        print("LT 1")
        sleep(5)
        print("hello")

    def LongTaskDone(self, result):
        print("ended 1")
        self.startButton.Enable()

class hey(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

以下,LongTask btn处理5s,btn1单独处理5s。全部加在一起将在8-9秒内给出结果。如何在5秒内获得所有结果?

from time import sleep
import wx
import wx.lib.agw.aui as aui
from wx.lib.delayedresult import startWorker
import serial
import matplotlib.pyplot as plt
from matplotlib.pyplot import subplot
from matplotlib.figure import Figure                                                
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas 
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar  
import threading 

class MainWindow(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__ ( self, parent )

        self.panel = wx.Panel(self)
        self.startButton = wx.Button(self.panel, label="Long Task")
        self.Button1 = wx.Button(self.panel, label="btn1")

        self.startButton.Bind(wx.EVT_BUTTON, self.OnStartButton)
        self.Button1.Bind(wx.EVT_BUTTON, self.OnBtn)

        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, 1, wx.ALL|wx.EXPAND )

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.startButton)
        self.sizer.Add(self.Button1)
        self.sizer.Add((10, 10))

        self.border = wx.BoxSizer()
        self.border.Add(self.sizer, 1, wx.ALL|wx.EXPAND , 5)

        #in attached file 
        self.x=[50144.0.... #[1]
        self.y=[11.0, ...   #[1]
        self.z=[111.3, ...  #[1]


        self.panel.SetSizerAndFit(self.border)  
        self.SetSizerAndFit(self.windowSizer)   


        self.panel2 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.panel2.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_ACTIVECAPTION ) )

        self.border.Add(self.panel2, 1, wx.ALL | wx.EXPAND, 5)

        ############################################ AUI windows
        self._mgr = aui.AuiManager()
        self._mgr.SetManagedWindow(self)
        self._mgr.AddPane(self.panel, aui.AuiPaneInfo().Name("tb1").Caption("Toolbar").ToolbarPane().Right())
        self._mgr.AddPane(self.panel2, aui.AuiPaneInfo().CenterPane().Caption("Sabit"))
        self._mgr.SetDockSizeConstraint(0.3, 0.65) #(w,h)
        self._mgr.Update()
        ############################################


    def OnStartButton(self, e):
        self.startButton.Disable()

        ##################################### aui-canvas
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        self.panel5 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer14 = wx.BoxSizer( wx.VERTICAL )
        self.figure2 = Figure((5,4), 85)
        self.axes2 = self.figure2.add_subplot(111)
        self.canvas2 = FigureCanvas(self.panel5, -1, self.figure2)
        self.toolbar2 = NavigationToolbar(self.canvas2) 
        self.toolbar2.Realize()
        bSizer14.Add(self.canvas2, 0, wx.ALL, 5 )
        bSizer14.Add(self.toolbar2, 0, wx.ALL, 5 )

        self.panel5.SetSizer( bSizer14 )
        self.panel5.Layout()
        bSizer14.Fit( self.panel5 )
        bSizer1.Add( self.panel5, 1, wx.ALL, 5 )   

        self._mgr.AddPane(self.panel5, aui.AuiPaneInfo().Bottom().Caption("Start"))
        self._mgr.Update()
        ##################################### aui

        startWorker(self.LongTaskDone, self.LongTask)

    def OnBtn(self, e):
        ##################################### aui-canvas
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        self.panel50 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        bSizer14 = wx.BoxSizer( wx.VERTICAL )
        self.figure20 = Figure((5,4), 85) 
        self.axes20 = self.figure20.add_subplot(111)
        self.canvas20 = FigureCanvas(self.panel50, -1, self.figure20)
        bSizer14.Add(self.canvas20, 0, wx.ALL, 5 )

        self.panel50.SetSizer( bSizer14 )
        self.panel50.Layout()
        bSizer14.Fit( self.panel50 )
        bSizer1.Add( self.panel50, 0, wx.ALL, 5 )  

        self._mgr.AddPane(self.panel50, aui.AuiPaneInfo().Bottom().Caption("Btn"))
        self._mgr.Update()
        ##################################### aui-canvas

        self.axes20.bar(self.x, self.z)
        self.canvas20.draw() 

    def LongTask(self):
        self.axes2.bar(self.x, self.y)
        self.canvas2.draw()

    def LongTaskDone(self, result):
        print("ended 1")
        self.startButton.Enable()

    def thread():

        thread1 = threading.Thread(target = OnStartButton)
        thread2 = threading.Thread(target = OnBtn) 

        thread1.start()
        thread2.start()


app = wx.App(False)
win = MainWindow(None)
win.Show(True)
app.MainLoop()
[1]: https://www.mediafire.com/file/yc2a52cyhvsu5hl/x-y-z.txt/file

0 个答案:

没有答案