跳到主要內容

[Python] 管理多個執行緒(thread)的運作與結束


在開發應用程式中,幾乎所有的專案都會用到thread,不同的程式語言有不同˙的叫用方法,但都大同小異。Thread的使用上最重要的不是在產生一個thread,而是如何結束一個thread,或是如何結束好幾個threads,以下介紹在Python中如何叫用並管理一或多個thread:

一般來說,create thread如下:

def job():
  while 1:
    print("Child thread:", i)
    time.sleep(1)

#In main code
thread = threading.Thread.__init__(job)
thread.start()
這樣可以開始一個thread執行job()中的動作,但若job中的迴圈是如上範例的無限迴圈,必須要加上能讓其退出的機制:

stopped = 0

def job():
  global stopped
  while stopped == 0:
    print("Child thread:", i)
    time.sleep(1)

#In main code
thread = threading.Thread.__init__(job)
thread.start()
這樣在主程序先將stopped設成1,再調用

thread.join()
就可以將thread完整關閉並釋放記憶體空間。

多執行續的處理:
基於這樣的方式,下面是進化版本,創建一個thread專用的class,並同時處理多個thread的執行與結束:
class EventHandleThread (threading.Thread):
    def __init__(self, fun, name):
        threading.Thread.__init__(self)
        self.name = name
        self.fun = fun
        self.stopped = [0]
    def run(self):
        self.fun(self.name, self.stopped)
        print ("Exiting " + self.name)
    def stop(self):
        self.stopped[0] = 1
在主程序中開始兩個thread,並加入m_thread的list中:
global m_Threads
thread1 = EventHandleThread(EventHandleLoop, "Thread-EventHandle")
m_Threads.append(thread1)
m_Threads[0].start();
thread2 = EventHandleThread(UiHandleLoop, "Thread-UiHandleLoop")
m_Threads.append(thread2)
m_Threads[1].start();
在程序結束前將所有thread的stopped設為1(叫用stop() function)

    def closeAllThreads(self):
        global m_Threads
        for i in range(len(m_Threads)):
            print ('stop main thread...')
            try:
                if m_Threads[i].is_alive():
                    m_Threads[i].stop()
                    m_Threads[i].join()
            except:
                print ('main thread except')
                pass

留言

這個網誌中的熱門文章

[Python] 以.py檔案產生Windows EXE 執行檔

如何開發能在Windows下執行的Python程式?你當然可以在Windows下安裝Python之後用command line運行你的 .py source code來直接執行程式,但是這方法基本上只適合開發中的程式,或是自己寫好玩只在自己的PC上執行的程式,也許適用。 但若你的需求是要在每個Windows的OS下都可以執行、且是不公開程式碼的,那就必須要將你的 .py source code包裝成EXE執行檔了,下面就介紹如何進行: –下載 auto-py-to-exe-master https://github.com/brentvollebregt/auto-py-to-exe 1. 解壓縮auto-py-to-exe-master.zip到想要的路徑下(路徑不能有空格或".") 2. 開啟Cmd console 到auto-py-to-exe-master路徑下 3. 在console下執行"pip install -r requirements.txt" 4. 在console下執行"python run.py" 運行完畢後就可以選取你要轉成執行檔的 .py 檔案,進行製作。 可參考下面的影片: 產生出來的檔案包含執行檔與其程式中會用到的相關Lib檔案,所以要將整個output資料夾中的檔案一併提取出來 (不要只拿執行檔出來)。 如此你就可以在任意的Windows上執行你的程式了。

Visual Studio 軟體開發的授權問題

使用Visual Studio開發各種軟體產品非常的方便,此款地表最強軟體開發工具涵蓋了各種的程式語言。但在授權上需要注意一下,否則很有可能會被微軟收取罰金。         Visual Studio Community可以在任何狀況下使用,但是要商業用途(賣產品)只限個人創作,公司組織的話除非是開放原碼、教育、學術研究,除此之外皆不可使用。 因此公司組織要用Visual Studio做產品的話,一種做法是買一套Professional版本,開發時用 Community 版本,要Release給客戶時用 Professional 或更高級的版本編譯;另一種做法是不考慮轉用更高級版本,那麼一開始就採用 Express 版可以降低未來轉換之困擾。 但需要知道 Express 版少了許多功能(例如 Code Coverage、Profiling ...),並且從2019開始微軟不再推出Express版本,也意味著沒有"公司組織免費使用Visual Studio開發產品"的選項了。 微軟雖然不再推出Express版本,但有另一個免費的IDE工具VS code,也可用來開發軟體,只是相對來說Visual Studio只稱得上是一個程式編輯器,如果要在VS code上開發如Winform的程式,就要使用第三方的工具了,以下提供幾個VS code上開發的連結: VS code上開發Winform https://ironmansoftware.com/building-a-windows-form-app-with-powershell-in-vs-code/ VS code上開發ASP.NET core https://blog.johnwu.cc/article/asp-net-core-3-starting.html