午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

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

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

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

此代碼將執行以下操作:

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。

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

一旦有了包含函數調用和超時的對象,只需定期檢查函數是否準備就緒即可執行。

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

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

此代碼將執行以下操作:

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循環中進行更多操作,而不僅僅是檢查超時功能是否準備就緒。您可能正在輪詢用戶輸入,控制某些硬件,讀取數據等。

解決方法

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

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

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

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 亚洲国产成人精品女人久久久 | www.精品一区 | 日韩在线视频网址 | 亚洲成a人片在线 | 欧美一区二区在线观看视频 | 国内精品一区二区 | 成人亚洲国产 | 亚洲第一免费播放区 | 国产精品成av人在线视午夜片 | 麻豆md0077饥渴少妇 | 人人草在线视频 | 可以在线看的av | 色婷综合 | 男人天堂网av | 国产午夜不卡 | 噜噜噜影院 | 激情开心成人网 | 美日韩中文字幕 | 日韩精品一区二区三区在线播放 | 成人一区二区视频 | 深夜毛片 | 亚洲做受高潮无遮挡 | 亚洲偷偷 | 超碰在线9 | 超碰人人超碰 | 免费不卡视频 | 欲色天天| 日韩福利网站 | 波多野结衣久久精品 | 国产精品久久久久久久午夜 | 国产黄色免费网站 | 青青草黄色 | 91精品国产综合久久香蕉922 | 伊是香蕉大人久久 | 免费视频一二三区 | 亚洲视频在线看 | 奇米影视久久 | 成人午夜网| 日韩三级在线观看视频 | 一级性爱视频 | 国产成人a亚洲精品 |