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 Show 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 TimerCú pháp:
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:
Kết quả in ra là:
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 timerCú pháp:
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
Kết quả in ra là:
Nguồn và Tài liệu tiếng anh tham khảo:
Tài liệu từ cafedev:
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:
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 PythonA 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
PrerequisitesTo follow along the reader will need the following:
Python timer functionsAfter 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).
Instance 1
Instance 2In this second example, I will show you how to implement the suspend method
Threading module overviewThe 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:
Creating and using the timer classThe 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.
Working with Python DecoratorWhile 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.
When the code is run the output should be:
Importance of using Threads
ConclusionIn this article we have learned the following:
Enjoy timing your threads. Peer Review Contributions by: Odhiambo Paul |