In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use Time". In daily operation, I believe many people have doubts about how to use Time. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope it will help you answer the doubts about "how to use Time"! Next, please follow the small series to learn together!
Let's look at the simple use of Time.
import timefrom 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)
The result of executing the above code is:
tm21===== 1.0052900314331055start=======okt21==== 3.0050549507141113
The above code prints tm21 and executes async_test_timer, the Timer class indicates that an action should run after a certain amount of time-that is, a timer. Timer is a subclass of Thread, so you can also create custom threads using functions. It is asynchronous and convenient for time-consuming operations. The first parameter is the number of seconds to execute, the second parameter is the function name, and the third parameter is the parameter required for the function to execute asynchronously.
Attached below is the Timer class source code, which inherits the Thread class.
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)
At this point, the study of "how to use Time" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.