【问题标题】:How to control/call another python script within one python script? (Communicate between scripts)如何在一个 python 脚本中控制/调用另一个 python 脚本? (脚本之间的通信)
【发布时间】:2010-08-14 05:13:45
【问题描述】:

我正在开发一个 GUI 程序,并且打算在一个事件中添加一个长时间运行的任务,但我发现这会使整个程序冻结很多,所以考虑到其他人的建议,我会让 GUI 只负责启动、停止和监视,并使长时间运行的任务作为单独的脚本运行。我知道在一个脚本中运行另一个脚本的唯一方法是通过导入,是否有任何其他方法可以与另一个脚本通信,我的意思是例如读取另一个的标准输出并随时终止它?

【问题讨论】:

    标签: python event-handling wxpython


    【解决方案1】:

    我建议你看看threading module。通过继承Thread 类,您可以为时间密集型作业创建新线程。

    然后,对于线程之间的通信,您可以使用pubsubpydispatcher,我没有尝试过后者,所以我无法对此发表评论,但我推荐 pubsub 的易用性和事实它的 wxpython 的一部分是一个奖励。

    Here 是一个关于运行长任务的 wxpython wiki 页面,如果您想要最简单的线程示例用法,请跳到最后。


    这是一个简单(可运行)的示例,说明如何使用 pubsub 将消息从您的 workerThread 发送到您的 GUI

    import time
    
    import wx
    from threading import Thread
    from wx.lib.pubsub import Publisher
    
    class WorkerThread(Thread):
        def __init__(self):
            Thread.__init__(self)
    
            #A flag that can be set 
            #to tell the thread to end
            self.stop_flag = False
    
            #This calls the run() to start the new thread
            self.start()
    
    
        def run(self):
            """ Over-rides the super-classes run()"""
            #Put everything in here that 
            #you want to run in your new thread
    
            #e.g...
            for x in range(20):
                if self.stop_flag:
                    break
                time.sleep(1)
                #Broadcast a message to who ever's listening
                Publisher.sendMessage("your_topic_name", x)
            Publisher.sendMessage("your_topic_name", "finished")
    
    
        def stop(self):
            """
            Call this method to tell the thread to stop
            """
            self.stop_flag = True
    
    
    
    
    class GUI(wx.Frame):
        def __init__(self, parent, id=-1,title=""):
            wx.Frame.__init__(self, parent, id, title, size=(140,180))
            self.SetMinSize((140,180)) 
            panel = wx.Panel(id=wx.ID_ANY, name=u'mainPanel', parent=self)
    
            #Subscribe to messages from the workerThread
            Publisher().subscribe(self.your_message_handler, "your_topic_name")
    
            #A button to start the workerThread
            self.startButton = wx.Button(panel, wx.ID_ANY, 'Start thread')
            self.Bind(wx.EVT_BUTTON,  self.onStart, self.startButton)
    
            #A button to stop the workerThread
            self.stopButton = wx.Button(panel, wx.ID_ANY, 'Stop thread')
            self.Bind(wx.EVT_BUTTON,  self.onStop, self.stopButton)
    
            #A text control to display messages from the worker thread
            self.threadMessage = wx.TextCtrl(panel, wx.ID_ANY, '', size=(75, 20))
    
            #Do the layout
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(self.startButton, 0, wx.ALL, 10)
            sizer.Add(self.stopButton, 0, wx.ALL, 10)
            sizer.Add(self.threadMessage, 0, wx.ALL, 10)
            panel.SetSizerAndFit(sizer)
    
    
        def onStart(self, event):
            #Start the worker thread
            self.worker = WorkerThread()
    
            #Disable any widgets which could affect your thread
            self.startButton.Disable()
    
        def onStop(self, message):
            self.worker.stop()
    
        def your_message_handler(self, message):
            message_data = message.data
            if message_data == 'finished':
                self.startButton.Enable()
                self.threadMessage.SetLabel(str(message_data))
            else:
                self.threadMessage.SetLabel(str(message_data))
    
    if __name__ == "__main__":
    
        app = wx.PySimpleApp()
        frame = GUI(None, wx.ID_ANY, 'Threading Example')
        frame.Show()
        app.MainLoop()
    

    【讨论】:

    • 我将使用最简单的方法,就像上一个示例一样。非常感谢!
    • 不用担心。我刚刚添加了一个可运行的示例,说明如果您决定走这条路线,如何使用 pubsub 进行通信
    【解决方案2】:

    如果您需要通过标准输入/标准输出进行通信,则应使用subprocess 模块。

    【讨论】:

      【解决方案3】:

      有比阅读标准输出更好的方法:http://docs.python.org/library/multiprocessing.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 2022-11-20
        • 2013-06-01
        • 2017-03-30
        相关资源
        最近更新 更多