Hướng dẫn python threading timer

Tuy nhiên, khoảng thời gian được khởi tạo cho timer có thể sẽ không phải là tức thời, khi hành động thực sự được thực hiện bởi trình thông dịch, bởi vì việc thực sự lập lịch cho thread tương ứng của timer object là trách nhiệm của thread scheduler – trình lập lịch cho các luồng

Timer là một sub-class (lớp con) của class Thread được định nghĩa trong Python. Nó được khởi động bằng cách gọi đến hàm start() tương ứng với timer.

1. Tạo ra một đối tượng Timer

Cú pháp:

threading.Timer(interval, function, args = None, kwargs = None)

Cú pháp trên sẽ tạo ra một timer, timer này sẽ khởi chạy hàm function với các đối số args và các đối số từ khóa (keyword arguments) kwargs, sau khi khoảng thời gian interval (đơn vị thời gian ở đây được dùng là giây) đã trôi qua. Nếu args là None (chính à giá trị mặc định của nó), thì một danh sách trống (empty list) sẽ được sử dụng. Nếu kwargs là None (chính là giá trị mặc định của nó), thì một từ điển trống (empty dict) sẽ được sử dụng.

Dưới đây là đoạn chương trình Python mô tả cách sử dụng timer objects trong Python:

# -----------------------------------------------------------
#Cafedev.vn - Kênh thông tin IT hàng đầu Việt Nam
#@author cafedevn
#Contact: 
#Fanpage: https://www.facebook.com/cafedevn
#Group: https://www.facebook.com/groups/cafedev.vn/
#Instagram: https://instagram.com/cafedevn
#Twitter: https://twitter.com/CafedeVn
#Linkedin: https://www.linkedin.com/in/cafe-dev-407054199/
#Pinterest: https://www.pinterest.com/cafedevvn/
#YouTube: https://www.youtube.com/channel/UCE7zpY_SlHGEgo67pHxqIoA/
# -----------------------------------------------------------

# Program to demonstrate 
# timer objects in python 
  
import threading 
def gfg(): 
    print("Cafedev\n") 
  
timer = threading.Timer(2.0, gfg) 
timer.start() 
print("Exit\n") 

Kết quả in ra là:

Exit
Cafedev

Giải thích: Đoạn chương trình trên sẽ lập lịch cho hàm gfg() chạy sau khoảng thời gian là 5 giây, kể từ khi hàm start() được gọi.

2. Hủy đi một timer

Cú pháp:

timer.cancel()

Câu lệnh này sẽ dừng timer lại, và hủy việc thực thi  hành động mà timer này đang thực hiện. Câu lệnh này sẽ chỉ làm việc nếu timer vẫn đang ở trong waiting stage – giai đoạn chờ đợi.

Dưới đây là đoạn chương trình Python mô tả cách hủy đi một (đối tượng) timer


# Program to cancel the timer 
import threading 
  
def gfg(): 
    print("GeeksforGeeks\n") 
  
timer = threading.Timer(5.0, gfg) 
timer.start() 
print("Cancelling timer\n") 
timer.cancel() 
print("Exit\n") 

Kết quả in ra là:

Cancelling timer
Exit

Nguồn và Tài liệu tiếng anh tham khảo:

  • w3school
  • python.org
  • geeksforgeeks

Tài liệu từ cafedev:

  • Full series tự học Python từ cơ bản tới nâng cao tại đây nha.
  • Ebook về python tại đây.
  • Các series tự học lập trình khác

Nếu bạn thấy hay và hữu ích, bạn có thể tham gia các kênh sau của cafedev để nhận được nhiều hơn nữa:

  • Group Facebook
  • Fanpage
  • Youtube
  • Instagram
  • Twitter
  • Linkedin
  • Pinterest
  • Trang chủ

Chào thân ái và quyết thắng!

Đăng ký kênh youtube để ủng hộ Cafedev nha các bạn, Thanks you!

Threading allows multiple tasks to run concurrently. For example, when task A is running, I do not have to wait for it to complete. Meanwhile, tasks B, C will also be running. When the tasks are running simultaneously, they require multiple CPUs.

To run threads concurrently Python uses a technique known as task switching. As a result, Python switches between each task rapidly. Making it seems like multiple tasks are running in parallel, making it useful in event-driven tasks. The thread being lightweight, it requires less memory thereby saving on CPU resources.

How to perform threading timer in Python

A thread has an entry, an execution, and an exit point. The Python library contains a timer, a subclass of the “threading” class used for code execution after a limited period.

Threading in Python Timer() starts following the delay defined as an argument. The Timer class thus calls itself delaying the execution of the following operation by the same amount of time specified.

Table of contents

  • How to Perform Threading Timer in Python
  • Table of Contents
  • Prerequisites
  • Python Timer Functions
    • Instance #1
    • Instance #2
  • Threading Module Overview
  • Creating and Using Timer Class
  • Working with Python Decorator
  • Importance of using Threads
  • Conclusion

Prerequisites

To follow along the reader will need the following:

  • Some basic knowledge in Python. You can refer to this article beginners guide to python to get started with Python.

Python timer functions

After every specified number of seconds, a timer class function is called. start() is a function that is used to initialize a timer. To end or quit the timer, one must use a cancel() function. Importing the threading class is necessary for one to use the threading class. The calling thread can be suspended for seconds using the function time.sleep(secs).

  • To better understand this, I will be illustrating it with the use of a code snippet and also with the expected output inform of a screenshot.

Instance 1

## How class threading.Timer() in python works  
import threading as th  
## Defining a method  
def sctn():  
   print("SECTION FOR LIFE \n")  
S = th.Timer(5.0, sctn)  
S.start()  
print("Exit Program\n")

  • After the code is run, it takes five minutes to display SECTION FOR LIFE as the output.

Hướng dẫn python threading timer

Instance 2

In this second example, I will show you how to implement the suspend method cancel(), which we had seen earlier to end a thread.

##Illustrating the use of cancel() method in class Timer.  
import threading as th  
## Defining of a method  
def sctn():  
    print("SECTION FOR LIFE \n")  
S = th.Timer(5.0, sctn)  
S.start()  
print("PROGRAM TERMINATION\n")  
S.cancel()

  • When the program is executed, the line PROGRAM TERMINATION is displayed. This is because the object th.Timer gets canceled just before it has executed the “sctn” function.
  • Below is the output of the above program:

Hướng dẫn python threading timer

Threading module overview

The latest threading module included with the current Python 2.4 provides a much more powerful and higher-level support for threads than the previous thread module.

The threading module exposes all the methods of the thread module and provides some additional functions as depicted below:

  thread.activeCount() − Returns how many thread objects are active.
  thread.currentThread() − Returns how many thread objects in the caller's thread control.
  thread.enumerate() − Returns an overview list of all thread objects that are currently active.

Creating and using the timer class

The beauty of threading is that you can tell the computer to perform a task some other time or do it simultaneously. You can also execute the code simultaneously on different threads, making it extremely powerful. A timer class always runs in intervals.

The Python Timer class is used to perform an operation or have a function run after a specified period has passed. The threading class has a subclass called the class timer. In technical terms, we will create Timer objects when we need time-bound actions (methods), in technical terms.

To use Timer class we will first have to import the time module. The args parameter is always preferably used to declare arguments to the functions to be called.

##Timers  
##Execute code at timed intervals  
##Imports and Displays  
import time  
from threading import Timer  
def display(msg):  
    print(msg + ' ' + time.strftime('%H:%M:%S'))  
  
##Basic timer  
def run_once():  
    display('run_once:')  
    t=Timer(10,display,['Timeout:'])  
    t.start()#Here run is called  
run_once()  
##Runs immediately and once  
print('Waiting.....')  
  
##Lets make our timer run in intervals  
##Put it into a class  
##Making it run until we stop it  
##Just getting crazy.Notice We have multiple timers at once!  
class RepeatTimer(Timer):  
    def run(self):  
        while not self.finished.wait(self.interval):  
            self.function(*self.args,**self.kwargs)  
            print(' ')  
##We are now creating a thread timer and controling it  
timer = RepeatTimer(1,display,['Repeating'])  
timer.start() #recalling run  
print('Threading started')  
time.sleep(10)#It gets suspended for the given number of seconds  
print('Threading finishing')  
timer.cancel()

  • Below is the output:

Hướng dẫn python threading timer

Working with Python Decorator

While working with a Python decorator, will know how to extend the Python Timer for it to be reused. The importance of using decorators is that it gets implemented once, and the function gets timed every time.

  • To begin, we will have the Python Timer called before the decorated function, and after the call ends, terminate the Python Timer.

import functools  
import time  
  
 def timer(meth):  
    @functools.wraps(meth)  
    def timer_container(*args, **kwargs):  
        click = time.flow()  
        value: object = meth(*args, **kwargs)  
        clock = time.flow()  
        time_passed = click - clock ##getting the time spent
        print(f"TIME PASSED IS: {time_passed:0.2f} SECS")  ##displaying time passed to 2 decimal places
        return value  
  
    return timer_container()

When the code is run the output should be:

TIME PASSED IS: 0.59 SECS

Importance of using Threads

  • Threads can be operated concurrently, multithreaded programs can run quicker on computer systems with several CPUs.
  • A program can continue to respond to input. This is true on a single CPU as well as several CPUs.
  • Threads in a process can share global variable memory. When a global variable is modified in one thread, it affects all threads. Local variables can also exist in a thread.
  • Handling of threads in an operating system is easier than handling processes. As a result, they’re sometimes referred to as lightweight processes.
  • It can be interrupted hence allowing for high priority processes.
  • It can temporarily be put on hold (at times referred to as in sleeping mode) while other threads are running - this is called yielding.

Conclusion

In this article we have learned the following:

  1. Python Timer Functions: How to use functions such as cancel() to stop execution even before it starts.
  2. Creating and Using Timer Class: The Timer class is a subclass of class Thread
  3. Working with Python Decorator: The decorator is used once but the function gets timed on and on.

Enjoy timing your threads.


Peer Review Contributions by: Odhiambo Paul