Phần I của bài viết: //viblo.asia/p/python-sleep-how-to-add-time-delays-to-your-code-part-i-translated-6J3ZgP6glmB
Phần II của bài viết: //viblo.asia/p/python-sleep-cach-them-do-tre-vao-chuong-trinh-phan-ii-translated-ORNZq1O3Z0n
Adding a Python sleep[] Call With Async IO
Khả năng bất đồng bộ đã được thêm vào Python 3.4 và tập tính năng này ngày càng được phát triển mạnh mẽ. Lập trình bất đồng bộ là một kiểu lập trình song song cho phép bạn chạy đa tác vụ cùng một lúc. Khi một tác vụ hoàn thành, nó sẽ thông báo về thread chính.
asyncio
là module cho phép bạn thêm một lời gọi sleep[]
bất đồng bộ. Nếu bạn không quen với bản thực thi lập trình bất đồng bộ của python, hãy xem bài viết Async IO in Python: A Complete Walkthrough và Python Concurrency & Parallel Programming.
Đây là một ví dụ từ documentation của chính Python:
import asyncio
async def main[]:
print['Hello ...']
await asyncio.sleep[1]
print['... World!']
# Python 3.7+
asyncio.run[main[]]
Trong ví dụ này, bạn chạy main[]
và khiến nó ngủ 1 giây giữa hai lời gọi print[]
.
Còn đây là một ví dụ hấp dẫn hơn từ phần documentation Coroutines and Tasks:
import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
Trong đoạn code này, bạn tạo ra worker
import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
0 nhận số giây import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
1 để ngủ và import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
2 để in ra. Sau đó, bạn sử dụng từ khóa import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
3 để chờ hàm import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
0 chạy. import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
3 bắt buộc phải có vì import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
0 đã được đánh dấu là một hàm import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
7, vậy nên bạn không thể gọi nó giống như với hàm thông thường.Khi nào bạn chạy đoạn code, chương trình sẽ xử lý
import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
3 ba lần. Đoạn code sẽ chờ 1, 2 và 3 giây, tổng cộng là 6 giây. Bạn có thể viết lại đoạn code để các tác vụ chạy song song:import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
Bạn đang sử dụng khái niệm về các task mà bạn có thể tạo với
import asyncio
import time
async def output[sleep, text]:
await asyncio.sleep[sleep]
print[text]
async def main[]:
print[f"Started: {time.strftime['%X']}"]
await output[1, 'First']
await output[2, 'Second']
await output[3, 'Third']
print[f"Ended: {time.strftime['%X']}"]
# Python 3.7+
asyncio.run[main[]]
9. Khi bạn sử dụng task trong asyncio
, Python sẽ chạy các task bất đồng bộ. Do vậy, khi bạn chạy đoạn code bên trên, nó sẽ hoàn thành trong 3 giây thay vì 6.Adding a Python sleep[] Call With GUIs
Các ứng dụng command line không phải là nơi duy nhất bạn có thể cần sử dụng hàm sleep[]
. Khi bạn tại mội GUI, thi thoảng bạn sẽ cần thêm độ trễ. Ví dụ, bạn có thể tạo một ứng dụng FTP để download hàng triệu file nhưng bạn cần thêm một lời gọi sleep[]
giữa các batch để server của bạn tránh bị chết.
Code GUI sẽ chạy các công việc xử lý và vẽ trong thread chính gọi là event loop. Nếu bạn sử dụng
import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
3 trong code GUI, bạn sẽ block event loop. Ở khía cạnh nghĩa dùng, ứng dụng có thể treo. Người dùng không thể tương tác với ứng dụng trong khi nó ngủ với phương thức này. Trên Windows, bạn thậm chí có thể nhận được một cảnh báo.event loop. Nếu bạn sử dụng import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
3 trong code GUI, bạn sẽ block event loop. Ở khía cạnh nghĩa dùng, ứng dụng có thể treo. Người dùng không thể tương tác với ứng dụng trong khi nó ngủ với phương thức này. Trên Windows, bạn thậm chí có thể
nhận được một cảnh báo.May thay, có các phương thức khác bạn có thể sử dụng bên trong
import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
3. Trong các phần tiếp theo, bạn sẽ tìm hiểu cách thêm lời gọi sleep[]
trong cả Tkinter và wxPython.Sleeping in Tkinter
Nguồn: //realpython.com/python-sleep/
import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
6 thuộc thư viện chuẩn của Python. Nó có thể không có sẵn nếu bạn đang dùng Python phiên bản cài đặt sẵn trên Linux và Mac. Nếu bạn gặp lỗi import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
7 thì bạn cần tìm cách thêm nó vào hệ thống. Nhưng nếu bạn tự cài đặt Python, import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
6 sẽ sẵn sàng để sử dụng.Bạn sẽ bắt đầu với ví dụ sử dụng
import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
3. Chạy đoạn code này để xem điều gì xảy ra khi bạn thêm lệnh gọi sleep[]
sai cách:import tkinter
import time
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
b = tkinter.Button[text="click me", command=self.delayed]
b.pack[]
def delayed[self]:
time.sleep[3]
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
Một khi bạn chạy đoạn code trên, ấn nút trong GUI. Nút bấm sẽ khựng lại trong ba giây vì nó chờ sleep[]
hoàn thành. Nếu ứng dụng có các nút khác, bạn không thể click vào chúng. Bạn không thể đóng ứng dụng khi nó đang ngủ bởi vì nó không thể trả lời sự kiện đóng.
Để khiến
import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
6 ngủ đúng cách, bạn cần sử dụng import tkinter
import time
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
b = tkinter.Button[text="click me", command=self.delayed]
b.pack[]
def delayed[self]:
time.sleep[3]
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
3:import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
Ở đây, bạn tạo ra một ứng dụng có kích thước 400 x 400, không chứa widget. Tất cả những gì nó làm là hiển thị một frame. Sau đó, bạn gọi
import tkinter
import time
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
b = tkinter.Button[text="click me", command=self.delayed]
b.pack[]
def delayed[self]:
time.sleep[3]
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
4 mà ở đó import tkinter
import time
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
b = tkinter.Button[text="click me", command=self.delayed]
b.pack[]
def delayed[self]:
time.sleep[3]
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
5 tham chiếu tới đối tượng import tkinter
import time
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
b = tkinter.Button[text="click me", command=self.delayed]
b.pack[]
def delayed[self]:
time.sleep[3]
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
6 nhận hai tham trị:- Số millisecond để ngủ
- Phương thức để gọi khi việc ngủ kết thúc
Trong trường hợp này, ứng dụng của bạn sẽ in ra một chuỗi ra stdout sau 3 giây. Bạn có thể coi
import tkinter
import time
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
b = tkinter.Button[text="click me", command=self.delayed]
b.pack[]
def delayed[self]:
time.sleep[3]
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
3 như là import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
3 phiên bản import asyncio
import time
async def output[text, sleep]:
while sleep > 0:
await asyncio.sleep[1]
print[f'{text} counter: {sleep} seconds']
sleep -= 1
async def main[]:
task_1 = asyncio.create_task[output['First', 1]]
task_2 = asyncio.create_task[output['Second', 2]]
task_3 = asyncio.create_task[output['Third', 3]]
print[f"Started: {time.strftime['%X']}"]
await task_1
await task_2
await task_3
print[f"Ended: {time.strftime['%X']}"]
if __name__ == '__main__':
asyncio.run[main[]]
6 nhưng nó cũng có thể khả năng gọi một hàm sau khi việc ngủ kết thúc.Bạn có thể sử dụng chức năng này để cải thiện UX [trải nghiệm người dùng]. Bằng cách thêm một lệnh gọi sleep[]
, bạn có thể làm cho ứng dụng của bạn có cảm giác tải nhanh hơn và sau đó khởi động các tiến trình lâu hơn. Bằng cách này, người dùng sẽ không phải chờ ứng dụng được mở.
Sleeping in wxPython
Có hai khác biệt chính giữa
import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
1 và import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
2:
1 gồm rất nhiều widgetimport tkinter class MyApp: def __init__[self, parent]: self.root = parent self.root.geometry["400x400"] self.frame = tkinter.Frame[parent] self.frame.pack[] self.root.after[3000, self.delayed] def delayed[self]: print['I was delayed'] if __name__ == "__main__": root = tkinter.Tk[] app = MyApp[root] root.mainloop[]
1 hướng tới đặc tính đa nền tảngimport tkinter class MyApp: def __init__[self, parent]: self.root = parent self.root.geometry["400x400"] self.frame = tkinter.Frame[parent] self.frame.pack[] self.root.after[3000, self.delayed] def delayed[self]: print['I was delayed'] if __name__ == "__main__": root = tkinter.Tk[] app = MyApp[root] root.mainloop[]
Framework
import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
1 không được tích hợp sẵn vào Python vậy nên bạn phải tự tải chúng về. Nếu bạn không quan về import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
1, hãy kiểm tra How to Build a Python GUI Application With wxPython.Trong
import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
7, bạn có thể sử dụng import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
8 để thêm một lời gọi sleep[]
:import wx
class MyFrame[wx.Frame]:
def __init__[self]:
super[].__init__[parent=None, title='Hello World']
wx.CallLater[4000, self.delayed]
self.Show[]
def delayed[self]:
print['I was delayed']
if __name__ == '__main__':
app = wx.App[]
frame = MyFrame[]
app.MainLoop[]
Ở đây, bạn thực hiện kế thừa trực tiếp và sau đó gọi
import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
8. Hàm này nhận các tham số giống import tkinter
import time
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
b = tkinter.Button[text="click me", command=self.delayed]
b.pack[]
def delayed[self]:
time.sleep[3]
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
3 của Tkinter:- Số millisecond để ngủ
- Phương thức để gọi khi việc ngủ kết thúc
Khi bạn chạy đoạn code này, bạn sẽ thấy một cửa sổ trắng [blank] xuất hiện mà không có bất cứ widget nào. Sau 4 giây, bạn sẽ thấy chuỗi "I was delayed" được in ra stdout.
Một trong những lợi ích của việc sử dụng
import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
8 chính là thread-safe. Bạn có thể sử dụng phương thức này từ một thread để gọi một hàm ở trong ứng dụng import tkinter
class MyApp:
def __init__[self, parent]:
self.root = parent
self.root.geometry["400x400"]
self.frame = tkinter.Frame[parent]
self.frame.pack[]
self.root.after[3000, self.delayed]
def delayed[self]:
print['I was delayed']
if __name__ == "__main__":
root = tkinter.Tk[]
app = MyApp[root]
root.mainloop[]
1Loạt bài viết xin được kết thúc tại đây!
Nguồn: //realpython.com/python-sleep/