當前位置:
首頁 > 知識 > python threading中處理主進程和子線程的關係

python threading中處理主進程和子線程的關係

之前用Python的多線程,總是處理不好進程和線程之間的關係。後來發現了join和setDaemon函數,才終於弄明白。下面總結一下。

1.使用join函數後,主進程會在調用join的地方等待子線程結束,然後才接著往下執行。

join使用實例如下:

[python] view plain copy

  1. import

    time

  2. import

    random
  3. import

    threading
  4. class

    worker(threading.Thread):
  5. def

    __init__(self):
  6. threading.Thread.__init__(self)
  7. def

    run(self):
  8. t = random.randint(1,10)
  9. time.sleep(t)
  10. print

    "This is " + self.getName() + ";I sleep %d second."%(t)
  11. tsk = []
  12. for

    i

    in

    xrange(0,5):
  13. time.sleep(0.1)
  14. thread = worker()
  15. thread.start()
  16. tsk.append(thread)
  17. for

    tt

    in

    tsk:

  18. tt.join()
  19. print

    "This is the end of main thread."

運行結果如下:[python] view plain copy

  1. # python testjoin.py
  2. This

    is

    Thread-3;I sleep 2 second.
  3. This

    is

    Thread-1;I sleep 4 second.
  4. This

    is

    Thread-2;I sleep 7 second.
  5. This

    is

    Thread-4;I sleep 7 second.
  6. This

    is

    Thread-5;I sleep 7 second.
  7. This

    is

    the end of main thread.

這裡創建了5個子線程,每個線程隨機等待1-10秒後列印退出;主線程分別等待5個子線程結束。最後結果是先顯示各個子線程,再顯示主進程的結果。

2. 如果使用的setDaemon函數,則與join相反,主進程結束的時候不會等待子線程。

setDaemon函數使用實例:

[python] view plain copy

  1. import

    time
  2. import

    random
  3. import

    threading
  4. class

    worker(threading.Thread):
  5. def

    __init__(self):
  6. threading.Thread.__init__(self)
  7. def

    run(self):
  8. t = random.randint(1,10)
  9. time.sleep(t)
  10. print

    "This is " + self.getName() + ";I sleep %d second."%(t)
  11. tsk = []
  12. for

    i

    in

    xrange(0,5):
  13. time.sleep(0.1)
  14. thread = worker()
  15. thread.setDaemon(True)
  16. thread.start()
  17. tsk.append(thread)
  18. print

    "This is the end of main thread."

這裡設置主進程為守護進程,當主進程結束的時候,子線程被中止

運行結果如下:

[python] view plain copy

  1. #python testsetDaemon.py
  2. This

    is

    the end of main thread.

3、如果沒有使用join和setDaemon函數,則主進程在創建子線程後,直接運行後面的代碼,主程序一直掛起,直到子線程結束才能結束。

[python] view plain copy

  1. import

    time
  2. import

    random

  3. import

    threading
  4. class

    worker(threading.Thread):
  5. def

    __init__(self):
  6. threading.Thread.__init__(self)
  7. def

    run(self):
  8. t = random.randint(1,10)
  9. time.sleep(t)
  10. print

    "This is " + self.getName() + ";I sleep %d second."%(t)
  11. tsk = []
  12. for

    i

    in

    xrange(0,5):
  13. time.sleep(0.1)
  14. thread = worker()
  15. thread.start()
  16. tsk.append(thread)
  17. print

    "This is the end of main thread."

運行結果如下:[python] view plain copy

  1. # python testthread.py
  2. This

    is

    the end of main thread.

  3. This

    is

    Thread-4;I sleep 1 second.
  4. This

    is

    Thread-3;I sleep 7 second.
  5. This

    is

    Thread-5;I sleep 7 second.
  6. This

    is

    Thread-1;I sleep 10 second.
  7. This

    is

    Thread-2;I sleep 10 second.

python threading中處理主進程和子線程的關係

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

淺談WKWebView使用、JS的交互
python解包和壓包

TAG:程序員小新人學習 |