Số giây đã trôi qua trong phút này được tính từ date_time[-2. ]. Lấy 60 trừ đi giá trị này sẽ cho khoảng thời gian tính bằng giây mà vòng lặp sẽ ở chế độ ngủ, để thực thi khi phút tiếp theo bắt đầu
Khi vòng lặp đã ngủ trong số giây cần thiết, chúng tôi tra cứu lại ngày giờ và in nó ra
3 – Trình lập lịch tác vụ
Hai tùy chọn trước rất phù hợp để thực hiện một vòng lặp một vài lần, trong trường hợp của chúng tôi là mười lần. Nếu chúng tôi muốn thực thi tập lệnh python liên tục mà không hết hạn, chúng tôi có thể sử dụng các ví dụ trên với vòng lặp nối thêm vô hạn
Tuy nhiên, nếu một vòng lặp bị lỗi, tập lệnh sẽ dừng lại. Do đó, chúng tôi muốn thực thi toàn bộ tập lệnh Python mỗi phút một lần bằng trình kích hoạt bên ngoài. Đây là nơi chúng ta có thể sử dụng Trình lập lịch tác vụ
Trình lập lịch tác vụ có thể thực thi tập lệnh python từ nguồn nhưng sử dụng tệp bó thường dễ dàng hơn. Tệp bó bao gồm vị trí của ứng dụng python [python. exe] và vị trí của tập lệnh python [. py]. Để biết thêm chi tiết về cách sử dụng Trình lập lịch tác vụ và tệp bó để chạy tập lệnh Python, vui lòng xem bài đăng datatofish sau – https. // dữ liệu cá. com/python-script-windows-scheduler
Tệp bó của chúng tôi là
Để minh họa Trình lập lịch tác vụ, tôi sẽ chạy mã Python sau mỗi phút
Mã này sử dụng Pandas để tạo tệp CSV trống, nhưng tên của tệp CSV là ngày giờ tập lệnh được chạy
Các ảnh chụp màn hình sau đây hiển thị các trình kích hoạt và hành động được sử dụng
Điều này tạo ra các tệp CSV sau, chúng ta có thể thấy rằng các tệp mất 1 – 4 giây để tạo
Tóm lại, chúng ta đã thấy ba cách khác nhau để trì hoãn vòng lặp Python, hai cách sử dụng vòng lặp bên trong Python và một cách sử dụng Trình lập lịch tác vụ. Tất cả đều có thể được sử dụng tùy thuộc vào loại trì hoãn nào là tốt nhất
Ghi chú. Như một đầu ra, tôi đã hiển thị GIF, để bạn có thể nhận thấy thời gian trễ trong mã chương trình trong thời gian thực hiện
ví dụ 1. In các số bằng cách thêm thời gian trễ
Python3
# importing module
import
time
# running loop from 0 to 4
for
threading.Event.wait[]0_______0_______1
threading.Event.wait[]2
threading.Event.wait[]3
threading.Event.wait[]4
threading.Event.wait[]5
threading.Event.wait[]6
threading.Event.wait[]7
threading.Event.wait[]8
threading.Event.wait[]8
Geeks for Geeks0
threading.Event.wait[]8_______10_______2
Geeks for Geeks3
threading.Event.wait[]8
threading.Event.wait[]8
Geeks for Geeks6
threading.Event.wait[]8_______10_______8
Geeks for Geeks9
threading.Timer[interval, function]0
đầu ra
ví dụ 2. In ấn ấn tượng bằng cách sử dụng chế độ ngủ [] cho mọi ký tự
Python3
threading.Timer[interval, function]1
import
time
threading.Timer[interval, function]4
threading.Timer[interval, function]5
threading.Event.wait[]8
threading.Timer[interval, function]7_______224_______
threading.Event.wait[]0
threading.Event.wait[]1
Timer Computer Science: GeeksforGeeks1
Timer Computer Science: GeeksforGeeks2
Timer Computer Science: GeeksforGeeks3
Timer Computer Science: GeeksforGeeks4
Timer Computer Science: GeeksforGeeks3
Geeks for Geeks2
Timer Computer Science: GeeksforGeeks7
Timer Computer Science: GeeksforGeeks8
Timer Computer Science: GeeksforGeeks9
Timer Computer Science: GeeksforGeeks3
Timer Computer Science: GeeksforGeeks3
# importing module
2Timer Computer Science: GeeksforGeeks3
Geeks for Geeks8
# importing module
5threading.Timer[interval, function]0
# importing module
7
# importing module
8 # importing module
9
Timer Computer Science: GeeksforGeeks8
Timer Computer Science: GeeksforGeeks8
import
2import
3threading.Timer[interval, function]7_______221_______5
Timer Computer Science: GeeksforGeeks8
import
7threading.Timer[interval, function]7
threading.Timer[interval, function]7____222_______0
threading.Timer[interval, function]7_______222_______2
threading.Timer[interval, function]7_______222_______4
đầu ra
ví dụ 3. In mẫu bằng cách lấy phạm vi từ người dùng và thêm thời gian trễ
Python3
# importing module
import
time
time
8
threading.Timer[interval, function]4
# running loop from 0 to 4
0threading.Event.wait[]8
threading.Timer[interval, function]7_______224_______
threading.Event.wait[]0_______0_______1
threading.Event.wait[]2
threading.Event.wait[]3
threading.Event.wait[]4
# running loop from 0 to 4
9Timer Computer Science: GeeksforGeeks3
for
for
2_______0_______1 threading.Event.wait[]2
threading.Event.wait[]3
threading.Event.wait[]4
for
7for
8for
9threading.Event.wait[]7
threading.Event.wait[]01
threading.Event.wait[]02_______10_______2
threading.Event.wait[]3
threading.Event.wait[]05
threading.Event.wait[]06
Timer Computer Science: GeeksforGeeks8
threading.Event.wait[]08
threading.Timer[interval, function]0
threading.Event.wait[]02
threading.Event.wait[]02____0_______12
threading.Event.wait[]02_______10_______8
# importing module
5threading.Timer[interval, function]0
Timer Computer Science: GeeksforGeeks3
Geeks for Geeks2
threading.Event.wait[]3
threading.Event.wait[]08
threading.Timer[interval, function]0
# importing module
7
# importing module
8 # importing module
9
Timer Computer Science: GeeksforGeeks8
Timer Computer Science: GeeksforGeeks8
import
2import
3threading.Event.wait[]8
threading.Timer[interval, function]7_______0_______31
threading.Timer[interval, function]7_______0_______33
Timer Computer Science: GeeksforGeeks8
threading.Event.wait[]35
threading.Timer[interval, function]7_______10_______2
threading.Event.wait[]3
threading.Event.wait[]39
threading.Timer[interval, function]0
threading.Timer[interval, function]7
threading.Timer[interval, function]7_______0_______43
threading.Timer[interval, function]7_______0_______45
đầu ra
Ví dụ 4. Đa luồng sử dụng chế độ ngủ []
Python3
threading.Event.wait[]46
import
time
threading.Event.wait[]49
threading.Event.wait[]50_______221_______
threading.Event.wait[]52
threading.Event.wait[]53
threading.Event.wait[]54
threading.Event.wait[]55
threading.Event.wait[]8
threading.Timer[interval, function]7_______20_______4
threading.Event.wait[]59
threading.Event.wait[]60_______0_______7
Timer Computer Science: GeeksforGeeks3
for
threading.Event.wait[]64_______0_______1
threading.Event.wait[]2
threading.Event.wait[]3
threading.Event.wait[]35
threading.Event.wait[]7
threading.Event.wait[]02_______10_______2
threading.Event.wait[]3
threading.Event.wait[]73
threading.Timer[interval, function]0
threading.Event.wait[]02
threading.Event.wait[]02____0_______77
threading.Event.wait[]02_______10_______8
threading.Event.wait[]80
threading.Timer[interval, function]0
threading.Event.wait[]82
threading.Event.wait[]54
threading.Event.wait[]84
threading.Event.wait[]8
threading.Timer[interval, function]7_______20_______4
threading.Event.wait[]59
threading.Event.wait[]60_______0_______7
Timer Computer Science: GeeksforGeeks3
for
threading.Event.wait[]64_______0_______1
threading.Event.wait[]2
threading.Event.wait[]3
threading.Event.wait[]97
threading.Event.wait[]7
threading.Event.wait[]02_______10_______2
threading.Event.wait[]3
Geeks for Geeks02
threading.Timer[interval, function]0
threading.Event.wait[]02
threading.Event.wait[]02____10_______06
threading.Event.wait[]02_______10_______8
Geeks for Geeks09
threading.Timer[interval, function]0
Geeks for Geeks2_______0_______3
Geeks for Geeks13
threading.Timer[interval, function]0
Geeks for Geeks15
Geeks for Geeks16
Geeks for Geeks17____28_______8
Geeks for Geeks19
Geeks for Geeks20_______28_______8
Geeks for Geeks22
Geeks for Geeks23
Geeks for Geeks24
Geeks for Geeks25
Geeks for Geeks26
Geeks for Geeks27
Geeks for Geeks28
Geeks for Geeks29
Geeks for Geeks30
Geeks for Geeks31
Geeks for Geeks32
Geeks for Geeks2_______0_______3
Geeks for Geeks35
threading.Timer[interval, function]0
đầu ra
Phương pháp 2. sử dụng luồng. Biến cố. chức năng chờ
các luồng. Biến cố. thủ tục đợi, luồng đợi cho đến khi quá trình thực thi phương thức set[] chưa hoàn tất. Thời gian có thể được sử dụng trong đó;
Cú pháp của luồng là gì. Biến cố. chức năng chờ
threading.Event.wait[]
Thí dụ
Python3
threading.Event.wait[]49
time
import
Geeks for Geeks40
# importing module
8 # importing module
9
Timer Computer Science: GeeksforGeeks8
Timer Computer Science: GeeksforGeeks8
import
2import
3threading.Timer[interval, function]7_______10_______48
threading.Timer[interval, function]7_______10_______50
Timer Computer Science: GeeksforGeeks8
Geeks for Geeks9
threading.Timer[interval, function]7_______10_______2
threading.Event.wait[]3
Geeks for Geeks56
threading.Timer[interval, function]0
threading.Timer[interval, function]7_______10_______59
threading.Timer[interval, function]7_______10_______2
threading.Event.wait[]3
Geeks for Geeks63
threading.Timer[interval, function]0
threading.Timer[interval, function]7_______10_______59
threading.Timer[interval, function]7_______10_______2
threading.Event.wait[]3
Geeks for Geeks56
threading.Timer[interval, function]0
đầu ra
Geeks for Geeks
Phương pháp 2. sử dụng luồng. lớp hẹn giờ
Các hành động cần được lên lịch để bắt đầu tại một thời điểm cụ thể được biểu thị bằng các đối tượng hẹn giờ. Các mục này được lên lịch để thực thi trên một luồng khác thực hiện hành động