<del id="d4fwx"><form id="d4fwx"></form></del>
      <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

            <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
          • Python利用Timer定時(shí)器控制函數(shù)在特定時(shí)間執(zhí)行-創(chuàng)新互聯(lián)

            創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買(mǎi)多久送多久,劃算不套路!

            創(chuàng)新互聯(lián)建站專(zhuān)注于企業(yè)全網(wǎng)整合營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、清鎮(zhèn)網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開(kāi)發(fā)、商城系統(tǒng)網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為清鎮(zhèn)等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

            這篇文章將為大家詳細(xì)講解有關(guān)Python 利用Timer定時(shí)器控制函數(shù)在特定時(shí)間執(zhí)行,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

            Thread類(lèi)有一個(gè)Timer子類(lèi),該子類(lèi)可用于控制指定函數(shù)在特定時(shí)間內(nèi)執(zhí)行一次。例如如下程序:

            from threading import Timer
            
            def hello():
                print("hello, world")
            # 指定10秒后執(zhí)行hello函數(shù)
            t = Timer(10.0, hello)
            t.start()

            上面程序使用 Timer 控制 10s 后執(zhí)行 hello 函數(shù)。

            需要說(shuō)明的是,Timer 只能控制函數(shù)在指定時(shí)間內(nèi)執(zhí)行一次,如果要使用 Timer 控制函數(shù)多次重復(fù)執(zhí)行,則需要再執(zhí)行下一次調(diào)度。

            如果程序想取消 Timer 的調(diào)度,則可調(diào)用 Timer 對(duì)象的 cancel() 函數(shù)。例如,如下程序每 1s 輸出一次當(dāng)前時(shí)間:

            from threading import Timer
            import time
            
            # 定義總共輸出幾次的計(jì)數(shù)器
            count = 0
            def print_time():
                print("當(dāng)前時(shí)間:%s" % time.ctime())
                global t, count
                count += 1
                # 如果count小于10,開(kāi)始下一次調(diào)度
                if count < 10:
                    t = Timer(1, print_time)
                    t.start()
            # 指定1秒后執(zhí)行print_time函數(shù)
            t = Timer(1, print_time)
            t.start()

            上面程序開(kāi)始運(yùn)行后,程序控制 1s 后執(zhí)行 print_time() 函數(shù)。print_time() 函數(shù)中的代碼會(huì)進(jìn)行判斷,如果 count 小于 10,程序再次使用 Timer 調(diào)度 1s 后執(zhí)行 print_time() 函數(shù),這樣就可以控制 print_time() 函數(shù)多次重復(fù)執(zhí)行。

            在上面程序中,由于只有當(dāng) count 小于 10 時(shí)才會(huì)使用 Timer 調(diào)度 1s 后執(zhí)行 print_time() 函數(shù),因此該函數(shù)只會(huì)重復(fù)執(zhí)行 10 次。

            關(guān)于Python 利用Timer定時(shí)器控制函數(shù)在特定時(shí)間執(zhí)行就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

            新聞名稱(chēng):Python利用Timer定時(shí)器控制函數(shù)在特定時(shí)間執(zhí)行-創(chuàng)新互聯(lián)
            路徑分享:http://www.jbt999.com/article30/ccdipo.html

            成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、外貿(mào)建站、網(wǎng)站設(shè)計(jì)網(wǎng)站建設(shè)品牌網(wǎng)站設(shè)計(jì)、小程序開(kāi)發(fā)

            廣告

            聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

            成都定制網(wǎng)站建設(shè)

              <del id="d4fwx"><form id="d4fwx"></form></del>
              <del id="d4fwx"><form id="d4fwx"></form></del><del id="d4fwx"><form id="d4fwx"></form></del>

                    <code id="d4fwx"><abbr id="d4fwx"></abbr></code>
                  • 人妻av中文字幕 日本高清不卡视频 | 激情插入视频 | 中文在线а√在线 | 黄金网站免费大全入口 | 亚洲欧美日韩高清 |