An explanation of how to call a function at a specified time in python

巴扎黑
Release: 2017-09-09 10:36:23
Original
1772 people have browsed it

Function function is one of the core contents of python programming and is also a relatively important one. The following article mainly introduces you to the relevant information about the implementation of calling functions at specified times in python. The article introduces it in great detail through sample code. Friends who need it can refer to it. Let’s take a look together.

Preface

This article mainly introduces to you the relevant content about calling functions at specified time in Python, and shares it for your reference and study. The following words Not much to say, let’s take a look at the detailed introduction:

In the previous article, you can use delayed calling. Here, the specified time is used to call the function

The example is as follows:


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()
Copy after login

The result output is as follows:


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
Copy after login

In this example, call the function call_at() to implement this function.

The above is the detailed content of An explanation of how to call a function at a specified time in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template