亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

更多QQ空间微信QQ好友腾讯朋友复制链接
您的位置:首頁/技術(shù)文章
文章詳情頁

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

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

在實(shí)踐中,計(jì)時(shí)器可能是您要做的最簡單的方法。

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

1秒后,它會(huì)顯示“ arg1 arg2”2秒后,它會(huì)顯示“ 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()

===

上面的代碼很可能會(huì)解決您的問題。

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

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

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

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

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

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

1秒后,它會(huì)顯示“ 20”2秒后,它會(huì)顯示“ 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()

===

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

解決方法

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

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

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 久久婷婷色香五月综合激情 | 欧美一级毛片高清免费观看 | 8050午夜一级全黄毛片 | 国产激烈无遮挡免费床戏视频 | 亚洲天堂激情 | 巨乳毛片| 麻豆黄色 | 日韩欧美二区在线观看 | 91精品国产免费久久久久久青草 | 愉拍自拍 | 一级特黄视频 | 黄片毛片在线观看 | 国产欧美一区视频在线观看 | 欧美日韩在线成人 | 在线看免费观看韩国特黄一级 | 欧美一区二区三区不卡免费观看 | www黄色网 | 国产麻豆精品hdvideoss | 黄色大片黄色大片 | 色婷婷综合和线在线 | 日本在线观看免费看片 | 麻豆精品视频在线观看 | 高清一区二区三区免费 | 日韩综合第一页 | 免费播放观看视频大片 | 亚洲视频黄色 | 亚洲首页 | 日本黄色高清 | 色综合天天色 | 精品一久久香蕉国产二月 | 国产a级黄色毛片 | 亚洲无线一二三四手机 | 毛片在线看网站 | 国产v精品成人免费视频400条 | 久久亚洲精品一区二区三区浴池 | 亚洲一区二区欧美 | 毛色毛片免费看 | 久久免费视频在线 | 91视频第一页 | 欧美特黄一级大片 | 免费看一级黄色录像 |