本文將通過一個例子為大家分享python
中指定時間調(diào)用函數(shù)相關(guān)內(nèi)容,一起來看看吧,希望對大家
學習python有所幫助。
例子如下:
import asyncio
import time
def
callback(n, loop):
print('callback {} invoked at {}'.format(n, loop.time()))
async
def
main(loop):
now = loop.time()
print('clock time: {}'.format(time.time()))
print('loop time: {}'.format(now))
print('registering callbacks')
loop.call_at(now + 0.2, callback, 1, loop)
loop.call_at(now + 0.1, callback, 2, loop)
loop.call_soon(callback, 3, loop)
await asyncio.sleep(1)
event_loop = asyncio.get_event_loop()
try:
print('entering event loop')
event_loop.run_until_complete(main(event_loop))
finally:
print('closing event loop')
event_loop.close()
結(jié)果輸出如下:
entering event loop
clock time: 1504688585.2024202
loop time: 5747.015
registering callbacks
callback 3 invoked at 5747.031
callback 2 invoked at 5747.109
callback 1 invoked at 5747.218
closing event loop
在這個例子里調(diào)用函數(shù)call_at
(
)
來實現(xiàn)這個功能。
來源:
大坡3D
軟件開發(fā)