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.
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 là module cho phép bạn thêm một lời gọ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[]]
4 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
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[]]
5 và khiến nó ngủ 1 giây giữa hai lời gọ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[]]
6.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[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[]]
1Bạ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 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, 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
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[]]
4. 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 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[]]
4 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[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[]]
13 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[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[]]
13 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[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[]]
13 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[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[]]
13. Trong các phần tiếp theo, bạn sẽ tìm hiểu cách thêm lời gọ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[]]
4 trong cả Tkinter và wxPython.Sleeping in Tkinter
Nguồn: //realpython.com/python-sleep/
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[]]
16 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[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[]]
17 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[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[]]
16 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[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[]]
13. Chạy đoạn code này để xem điều gì xảy ra khi bạn thêm lệnh gọ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[]]
4 sai cách: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[]]
0Mộ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ờ
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[]]
4 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[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[]]
16 ngủ đúng cách, bạn cần sử dụng 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[]]
03: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Ở đâ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 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[]]
04 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[]]
05 tham chiếu tới đối tượng 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[]]
06 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 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[]]
03 như 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[]]
13 phiên bản 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[]]
16 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
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[]]
4, 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 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[]]
31 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[]]
32:
31 gồm rất nhiều widgetimport 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[]]
31 hướng tới đặc tính đa nền tảngimport 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[]]
Framework
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[]]
31 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 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[]]
31, hãy kiểm tra How to Build a Python GUI Application With wxPython.Trong
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[]]
37, bạn có thể sử dụng 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[]]
38 để thêm một lời gọ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[]]
4: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[]]
8Ở đây, bạn thực hiện kế thừa trực tiếp và sau đó gọ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[]]
38. Hàm này nhận các tham số giống 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[]]
03 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 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[]]
38 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 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[]]
31Loạt bài viết xin được kết thúc tại đây!
Nguồn: //realpython.com/python-sleep/