在线程结束之前,如何停止执行其他代码?

时间:2016-01-04 14:02:24

标签: python multithreading wxpython

[问题]
在线程结束之前,如何停止执行其他代码?。

实际上,我有一个wxpython。 (gui +业务逻辑)。     gui - gui.py     商业逻辑 :     a)main.py     b)specific.py

[gui.py]

import wx
app = wx.App(redirect=False)
top = wx.Frame(None, title="Hello World", size=(300,200))
def testcaseandLog(event):
        print "begin"
        import sys
        from main import mainPyBeginTest
        mainPyBeginTest()    
sizer = wx.GridBagSizer()
addButton = wx.Button( top, -1, "Run", style=wx.BU_EXACTFIT )
top.Bind(wx.EVT_BUTTON, testcaseandLog, addButton)
top.SetSizer(sizer)
top.SetFocus()
top.Center()
top.Show(True)
app.MainLoop()

[main.py]

def mainPyBeginTest():
        print "main py"
        import specific

[specific.py]

def testMethod():
        import wx
        appSecond = wx.App(redirect=False)
        topSecond = wx.Frame(None, title="Hello World", size=(300,200))
        import wx.lib.agw.pybusyinfo as PBI
        message = "Please wait, working..."
        busy = PBI.PyBusyInfo(message, parent=topSecond, title="processing")
        wx.Yield()
        for indx in xrange(5):
            wx.MilliSleep(1000)
        del busy
        topSecond.Show(True)
        topSecond.Destroy()
        appSecond.ExitMainLoop()
        appSecond.MainLoop()
def afterMethod():
     import threading
     t = threading.Thread(target=testMethod)
     t.setDaemon(1)
     t.start()
     import time
     time.sleep(10)
afterMethod()
def after():
    print "why is this gettting executed before testMethod"
after()

1 个答案:

答案 0 :(得分:0)

我很确定你想要join your thread

t = threading.Thread(target=testMethod)
t.setDaemon(1)
t.start()
t.join()  # This will cause the main thread to block until the 't' thread completes.