亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

將代碼推遲以供以后在python中執(zhí)行(例如javascript中的setTimeout)。

瀏覽:3日期:2022-08-07 14:15:06
如何解決將代碼推遲以供以后在python中執(zhí)行(例如javascript中的setTimeout)。?

在實踐中,計時器可能是您要做的最簡單的方法。

此代碼將執(zhí)行以下操作:

1秒后,它會顯示“ arg1 arg2”2秒后,它會顯示“ OWLS OWLS OWLS”

===

from threading import Timerdef twoArgs(arg1,arg2): print arg1 print arg2 print ''def nArgs(*args): for each in args:print each#arguments: #how long to wait (in seconds), #what function to call, #what gets passed inr = Timer(1.0, twoArgs, ('arg1','arg2'))s = Timer(2.0, nArgs, ('OWLS','OWLS','OWLS'))r.start()s.start()

===

上面的代碼很可能會解決您的問題。

但!還有另一種方法,不使用多線程。它的工作方式更像單線程的Javascript。

對于此單線程版本,您需要做的就是將函數(shù)及其參數(shù)存儲在一個對象中,以及應(yīng)該運行該函數(shù)的時間。

一旦有了包含函數(shù)調(diào)用和超時的對象,只需定期檢查函數(shù)是否準備就緒即可執(zhí)行。

正確的方法是使優(yōu)先級隊列存儲我們將來要運行的所有功能,如下面的代碼所示。

就像在Javascript中一樣,這種方法不能保證該函數(shù)將完全按時運行。運行時間很長的功能將延遲其后的功能。但是,它確實保證了函數(shù)將 不早于其超時運行。

此代碼將執(zhí)行以下操作:

1秒后,它會顯示“ 20”2秒后,它會顯示“ 132”3秒后,它退出。

===

from datetime import datetime, timedeltaimport heapq# just holds a function, its arguments, and when we want it to execute.class TimeoutFunction: def __init__(self, function, timeout, *args):self.function = functionself.args = argsself.startTime = datetime.Now() + timedelta(0,0,0,timeout) def execute(self):self.function(*self.args)# A 'todo' list for all the TimeoutFunctions we want to execute in the future# They are sorted in the order they should be executed, thanks to heapqclass TodoList: def __init__(self):self.todo = [] def addToList(self, tFunction):heapq.heappush(self.todo, (tFunction.startTime, tFunction)) def executeReadyFunctions(self):if len(self.todo) > 0: tFunction = heapq.heappop(self.todo)[1] while tFunction and datetime.Now() > tFunction.startTime:#execute all the functions that are readytFunction.execute()if len(self.todo) > 0: tFunction = heapq.heappop(self.todo)[1]else: tFunction = Noneif tFunction:#this one’s not ready yet, push it back onheapq.heappush(self.todo, (tFunction.startTime, tFunction))def singleArgFunction(x): print str(x)def multiArgFunction(x, y): #Demonstration of passing multiple-argument functions print str(x*y)# Make some TimeoutFunction objects# timeout is in millisecondsa = TimeoutFunction(singleArgFunction, 1000, 20)b = TimeoutFunction(multiArgFunction, 2000, *(11,12))c = TimeoutFunction(quit, 3000, None)todoList = TodoList()todoList.addToList(a)todoList.addToList(b)todoList.addToList(c)while True: todoList.executeReadyFunctions()

===

在實踐中,您可能會在while循環(huán)中進行更多操作,而不僅僅是檢查超時功能是否準備就緒。您可能正在輪詢用戶輸入,控制某些硬件,讀取數(shù)據(jù)等。

解決方法

我必須在python中執(zhí)行一個程序,該程序需要執(zhí)行一段時間,然后(無論在何處執(zhí)行)它必須將信息轉(zhuǎn)儲到文件中,關(guān)閉文件,然后退出。

在JavaScript中,此行為等同于使用setTimeout(func,1000000)其第一個參數(shù)(func)將是具有退出代碼的函數(shù)的指針,而其第二個參數(shù)將是程序可用的時間的用法。

我知道如何用C制作這個程序(使用SO信號),但是使用python

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久精品男人的天堂 | 一级毛片日韩 | 欧美aaa毛片免费看 欧美aaa视频 | 色综合色狠狠天天久久婷婷基地 | 日本护士视频xxxxxwww | 男人看片网址 | 牛牛本精品99久久精品88m | 精品99久久 | 免费a级毛片网站 | 99精品在线视频观看 | 大片在线播放日本一级毛片 | 男人女人做性全程视视频 | 国产精品夫妇久久 | 综合自拍 | 成人毛片高清视频观看 | chinese农村野战videos| 成人免费网站 | 免费观看视频成人国产 | 日韩一区二区三区在线播放 | 男女视频在线观看免费高清观看 | 国产午夜精品理论片久久影视 | 欧美高清在线视频在线99精品 | 特黄特色三级在线观看 | 久久草在线精品 | 久草视频福利在线观看 | 中文字幕一区在线 | 一个人看的免费高清视频日本 | 日本阿v精品视频在线观看 日本阿v视频在线观看高清 | 欧美一级特黄刺激爽大片 | 日韩精品永久免费播放平台 | 日韩国产在线播放 | 欧美最大成人毛片视频网站 | 久草网址 | 亚洲免费毛片 | 99国内视频 | 国产在线不卡午夜精品2021 | 亚洲人成在线播放网站 | 青久草视频| 久久欧美久久欧美精品 | 日韩国产中文字幕 | 成在线人永久免费播放视频 |