Time怎么使用
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,这篇文章主要介绍"Time怎么使用",在日常操作中,相信很多人在Time怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Time怎么使用"的疑惑有所帮助!接下来,
千家信息网最后更新 2025年02月03日Time怎么使用
这篇文章主要介绍"Time怎么使用",在日常操作中,相信很多人在Time怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Time怎么使用"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
先看下 Time 简单使用
import time
from threading import Timer
def test_timer():
print("start=======")
t1 = time.time()
time.sleep(3)
print("ok")
t2 = time.time()
t21 = t2 - t1
print('t21====', t21)
def async_test_timer():
Timer(1, test_timer, []).start()
if __name__ == "__main__":
tm1 = time.time()
time.sleep(1)
async_test_timer()
tm2 = time.time()
tm21 = tm2 - tm1
print('tm21=====', tm21)
上面代码的执行结果为:
tm21===== 1.0052900314331055start=======okt21==== 3.0050549507141113
上面的代码是先打印了 tm21 ,然后再执行了 async_test_timer 函数,Timer 类表示一个动作应该在一个特定的时间之后运行 - 也就是一个计时器。Timer是Thread的子类, 因此也可以使用函数创建自定义线程。它是异步的,用它处理耗时的操作很方便。第一个参数是多少秒后执行,第二个参数是函数名,第三个参数是要异步执行的函数所需的参数。
下面附 Timer 类的源码,它继承了 Thread 类。
class Timer(Thread):
"""Call a function after a specified number of seconds:
t = Timer(30.0, f, args=None, kwargs=None)
t.start()
t.cancel() # stop the timer's action if it's still waiting
"""
def __init__(self, interval, function, args=None, kwargs=None):
Thread.__init__(self)
self.interval = interval
self.function = function
self.args = args if args is not None else []
self.kwargs = kwargs if kwargs is not None else {}
self.finished = Event()
class Thread:
"""A class that represents a thread of control.
This class can be safely subclassed in a limited fashion. There are two ways
to specify the activity: by passing a callable object to the constructor, or
by overriding the run() method in a subclass.
"""
_initialized = False
# Need to store a reference to sys.exc_info for printing
# out exceptions when a thread tries to use a global var. during interp.
# shutdown and thus raises an exception about trying to perform some
# operation on/with a NoneType
_exc_info = _sys.exc_info
# Keep sys.exc_clear too to clear the exception just before
# allowing .join() to return.
#XXX __exc_clear = _sys.exc_clear
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, *, daemon=None):
"""This constructor should always be called with keyword arguments. Arguments are:
*group* should be None; reserved for future extension when a ThreadGroup
class is implemented.
*target* is the callable object to be invoked by the run()
method. Defaults to None, meaning nothing is called.
*name* is the thread name. By default, a unique name is constructed of
the form "Thread-N" where N is a small decimal number.
*args* is the argument tuple for the target invocation. Defaults to ().
*kwargs* is a dictionary of keyword arguments for the target
invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke
the base class constructor (Thread.__init__()) before doing anything
else to the thread.
"""
assert group is None, "group argument must be None for now"
if kwargs is None:
kwargs = {}
self._target = target
self._name = str(name or _newname())
self._args = args
self._kwargs = kwargs
if daemon is not None:
self._daemonic = daemon
else:
self._daemonic = current_thread().daemon
self._ident = None
self._tstate_lock = None
self._started = Event()
self._is_stopped = False
self._initialized = True
# sys.stderr is not stored in the class like
# sys.exc_info since it can be changed between instances
self._stderr = _sys.stderr
# For debugging and _after_fork()
_dangling.add(self)
到此,关于"Time怎么使用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
函数
参数
学习
代码
更多
帮助
实用
接下来
三个
也就是
动作
子类
文章
方法
时间
源码
理论
知识
篇文章
线程
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
邮件服务器登陆错误
杭州网络技术主要是干嘛的
数据库 显示表
春秋网络安全的痛楚时
怎么评估软件开发成本
速达v7服务器名在哪
网站和小程序服务器哪个好
郑州零点跳动网络技术有限公司
黑河网络安全会议
九义网络技术有限公司
2020网络安全创新百强
知到网课黑客文化与网络安全答案
网络安全软件测试论文
学了软件开发好难
数据库新增
数据库 session
广东深圳海讯网络技术
数据库dateset
河南网络安全周图片
天台销售软件开发解决方案
时间戳服务器的功能
服务器ping不通外网
数据库和土地资源管理论文
连南软件开发定制
全球互联网网络安全问题分析
网络安全员月薪多少钱一个月
大庆行业专业软件开发
浙江党员教育软件开发电话
数据库显示时间不对
腾讯星星海服务器规模