亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python 線程的暫停, 恢復(fù), 退出詳解及實(shí)例

 更新時(shí)間:2016年12月06日 11:52:20   作者:scolia  
這篇文章主要介紹了python 線程的暫停, 恢復(fù), 退出詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下

python 線程 暫停, 恢復(fù), 退出

我們都知道python中可以是threading模塊實(shí)現(xiàn)多線程, 但是模塊并沒有提供暫停, 恢復(fù)和停止線程的方法, 一旦線程對象調(diào)用start方法后, 只能等到對應(yīng)的方法函數(shù)運(yùn)行完畢. 也就是說一旦start后, 線程就屬于失控狀態(tài). 不過, 我們可以自己實(shí)現(xiàn)這些. 一般的方法就是循環(huán)地判斷一個(gè)標(biāo)志位, 一旦標(biāo)志位到達(dá)到預(yù)定的值, 就退出循環(huán). 這樣就能做到退出線程了. 但暫停和恢復(fù)線程就有點(diǎn)難了, 我一直也不清除有什么好的方法, 直到我看到threading中Event對象的wait方法的描述時(shí).

wait([timeout])

  Block until the internal flag is true. If the internal flag is true on entry, return immediately. Otherwise, block until another thread calls set() to set the flag to true, or until the optional timeout occurs.

  阻塞, 直到內(nèi)部的標(biāo)志位為True時(shí). 如果在內(nèi)部的標(biāo)志位在進(jìn)入時(shí)為True時(shí), 立即返回. 否則, 阻塞直到其他線程調(diào)用set()方法將標(biāo)準(zhǔn)位設(shè)為True, 或者到達(dá)了可選的timeout時(shí)間.


  When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof).

  This method returns the internal flag on exit, so it will always return True except if a timeout is given and the operation times out.

  當(dāng)給定了timeout參數(shù)且不為None, 它應(yīng)該是一個(gè)浮點(diǎn)數(shù),以秒為單位指定操作的超時(shí)(或是分?jǐn)?shù))。

  此方法在退出時(shí)返回內(nèi)部標(biāo)志,因此除非給定了超時(shí)且操作超時(shí),否則它將始終返回True。


  Changed in version 2.7: Previously, the method always returned None.

  2.7版本以前, 這個(gè)方法總會(huì)返回None.


  利用wait的阻塞機(jī)制, 就能夠?qū)崿F(xiàn)暫停和恢復(fù)了, 再配合循環(huán)判斷標(biāo)識(shí)位, 就能實(shí)現(xiàn)退出了, 下面是代碼示例:

#!/usr/bin/env python
# coding: utf-8

import threading
import time


class Job(threading.Thread):

  def __init__(self, *args, **kwargs):
    super(Job, self).__init__(*args, **kwargs)
    self.__flag = threading.Event()   # 用于暫停線程的標(biāo)識(shí)
    self.__flag.set()    # 設(shè)置為True
    self.__running = threading.Event()   # 用于停止線程的標(biāo)識(shí)
    self.__running.set()   # 將running設(shè)置為True

  def run(self):
    while self.__running.isSet():
      self.__flag.wait()   # 為True時(shí)立即返回, 為False時(shí)阻塞直到內(nèi)部的標(biāo)識(shí)位為True后返回
      print time.time()
      time.sleep(1)

  def pause(self):
    self.__flag.clear()   # 設(shè)置為False, 讓線程阻塞

  def resume(self):
    self.__flag.set()  # 設(shè)置為True, 讓線程停止阻塞

  def stop(self):
    self.__flag.set()    # 將線程從暫停狀態(tài)恢復(fù), 如何已經(jīng)暫停的話
    self.__running.clear()    # 設(shè)置為False  

下面是測試代碼:

a = Job()
a.start()
time.sleep(3)
a.pause()
time.sleep(3)
a.resume()
time.sleep(3)
a.pause()
time.sleep(2)
a.stop()

測試的結(jié)果:

 

  這完成了暫停, 恢復(fù)和停止的功能. 但是這里有一個(gè)缺點(diǎn): 無論是暫停還是停止, 都不是瞬時(shí)的, 必須等待run函數(shù)內(nèi)部的運(yùn)行到達(dá)標(biāo)志位判斷時(shí)才有效. 也就是說操作會(huì)滯后一次.

  但是這有時(shí)也不一定是壞事. 如果run函數(shù)中涉及了文件操作或數(shù)據(jù)庫操作等, 完整地運(yùn)行一次后再退出, 反而能夠執(zhí)行剩余的資源釋放操作的代碼(例如各種close). 不會(huì)出現(xiàn)程序的文件操作符超出上限, 數(shù)據(jù)庫連接未釋放等尷尬的情況.

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論