Cách tạm dừng giữa các bản in trong Python

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
Geeks
0

threading.Event.wait[]
8_______10_______2
Geeks
for
Geeks
3

threading.Event.wait[]
8

threading.Event.wait[]
8
Geeks
for
Geeks
6

threading.Event.wait[]
8_______10_______8
Geeks
for
Geeks
9
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: GeeksforGeeks
1

Timer
Computer Science: GeeksforGeeks
2

Timer
Computer Science: GeeksforGeeks
3
Timer
Computer Science: GeeksforGeeks
4

Timer
Computer Science: GeeksforGeeks
3
Geeks
for
Geeks
2
Timer
Computer Science: GeeksforGeeks
7
Timer
Computer Science: GeeksforGeeks
8
Timer
Computer Science: GeeksforGeeks
9

Timer
Computer Science: GeeksforGeeks
3

Timer
Computer Science: GeeksforGeeks
3# importing module2

Timer
Computer Science: GeeksforGeeks
3
Geeks
for
Geeks
8# importing module5
threading.Timer[interval, function]
0

 

 

# importing module7

# importing module8 # importing module9

Timer
Computer Science: GeeksforGeeks
8
Timer
Computer Science: GeeksforGeeks
8 import2import3

threading.Timer[interval, function]
7_______221_______5
Timer
Computer Science: GeeksforGeeks
8 import7

threading.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

 

 

time8

threading.Timer[interval, function]
4 # running loop from 0 to 40

threading.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 49

Timer
Computer Science: GeeksforGeeks
3for for2_______0_______1
threading.Event.wait[]
2
threading.Event.wait[]
3
threading.Event.wait[]
4for7for8for9
threading.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: GeeksforGeeks
8
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 module5
threading.Timer[interval, function]
0

Timer
Computer Science: GeeksforGeeks
3
Geeks
for
Geeks
2
threading.Event.wait[]
3
threading.Event.wait[]
08
threading.Timer[interval, function]
0

 

 

# importing module7

# importing module8 # importing module9

Timer
Computer Science: GeeksforGeeks
8
Timer
Computer Science: GeeksforGeeks
8 import2import3

threading.Event.wait[]
8

threading.Timer[interval, function]
7_______0_______31

threading.Timer[interval, function]
7_______0_______33
Timer
Computer Science: GeeksforGeeks
8
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: GeeksforGeeks
3for
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: GeeksforGeeks
3for
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
Geeks
02
threading.Timer[interval, function]
0

threading.Event.wait[]
02

threading.Event.wait[]
02____10_______06

threading.Event.wait[]
02_______10_______8
Geeks
for
Geeks
09
threading.Timer[interval, function]
0

 

 

Geeks
for
Geeks
2_______0_______3
Geeks
for
Geeks
13
threading.Timer[interval, function]
0

 

Geeks
for
Geeks
15

Geeks
for
Geeks
16

Geeks
for
Geeks
17____28_______8
Geeks
for
Geeks
19

Geeks
for
Geeks
20_______28_______8
Geeks
for
Geeks
22

 

Geeks
for
Geeks
23

Geeks
for
Geeks
24

 

Geeks
for
Geeks
25

Geeks
for
Geeks
26

 

Geeks
for
Geeks
27

Geeks
for
Geeks
28

Geeks
for
Geeks
29

Geeks
for
Geeks
30

 

Geeks
for
Geeks
31

Geeks
for
Geeks
32

Geeks
for
Geeks
2_______0_______3
Geeks
for
Geeks
35
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 timeimport
Geeks
for
Geeks
40

 

# importing module8 # importing module9

Timer
Computer Science: GeeksforGeeks
8
Timer
Computer Science: GeeksforGeeks
8 import2import3

threading.Timer[interval, function]
7_______10_______48

threading.Timer[interval, function]
7_______10_______50
Timer
Computer Science: GeeksforGeeks
8
Geeks
for
Geeks
9

 

threading.Timer[interval, function]
7_______10_______2
threading.Event.wait[]
3
Geeks
for
Geeks
56
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
Geeks
63
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
Geeks
56
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

Chủ Đề