Asyncio thu thập trăn

Phần I của bài viết. https. //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. https. //viblo. asia/p/trăn-ngủ-cach-them-do-tre-vao-chuong-trinh-phan-ii-dich-ORNZq1O3Z0n

Thêm cuộc gọi ngủ Python[] với Async IO

Bộ khả năng bất đồng đã được thêm vào Python 3. 4 và 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 thiết lập song song cho phép bạn chạy đa tác vụ cùng một lúc. Khi một nhiệm vụ hoàn thành, nó sẽ thông báo về chủ đề 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[]]
2 là mô-đun cho phép bạn 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[]]
3 bất đồng bộ. Nếu bạn không quen với bản thực thi thiết lập bất đồng bộ của python, hãy xem bài viết Async IO trong Python. Hướng dẫn hoàn chỉnh và Python Concurrency & Parallel Programming

Đây là một ví dụ từ tài liệu của Python chính

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[]]
4 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[]]
5

Còn đây là một ví dụ hấp dẫn khác từ phần tài liệu 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 mã 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 để vào. 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 giá 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ì vậy bạn không thể gọi nó giống như hàm thông thường

Khi nào bạn chạy đoạn mã, 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 mã sẽ đợi 1, 2 và 3 giây, tổng cộng là 6 giây. Bạn có thể viết lại đoạn mã để các tác vụ chạy song bài hát

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

Bạn đang sử dụng khái niệm về các nhiệm vụ mà bạn có thể tạo ra 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 tác vụ 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[]]
2, Python sẽ chạy các tác vụ bất đồng bộ. Do vậy, khi bạn chạy đoạn mã bên trên, nó sẽ hoàn thành trong 3 giây thay vì 6

Thêm một cuộc gọi ngủ Python[] với GUI

Các ứng dụng dòng lệnh không phải là nơi duy nhất mà 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[]]
3. When you at a GUI, this time you will need to more setting. Ví dụ, bạn có thể tạo một ứng dụng FTP để tải xuống hàng triệu tệp 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[]]
3 giữa các lô để máy chủ 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 gọi chính là vòng lặp sự kiện. 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 mã GUI, bạn sẽ chặn vòng lặp sự kiện. Ở cạnh nghĩa của ứng 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ó đang 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

Có thể thay, có các phương thức khác mà 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 thêm cách 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[]]
3 trong cả Tkinter và wxPython

Ngủ ở Tkinter

Nguồn. https. //trăn thật. com/trăn-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[]]
16 thuộc thư viện chuẩn của Python. Nó không thể khả dụng nếu bạn đang sử 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, thì
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

You will started with 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 mã 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[]]
3 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[]]
9

Một khi bạn chạy đoạn mã trên, nhấn nút trong GUI. Nút nhấn sẽ không bị chặn lại trong ba giây vì nó đang đợ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[]]
3 hoàn thành. Nếu ứng dụng có các nút khác, bạn không thể nhấp vào chúng. Bạn không thể đóng ứng dụng khi nó đang ngủ vì nó không thể trả lời sự kiện đóng

Để tạo ra

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[]]
93

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

Ở đâ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 đều hiển thị một khung. Sau đó, bạn gọi cho

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[]]
94 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[]]
95 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[]]
96 nhận hai tham trị

  1. Number millisecond to sleep
  2. Phương thức gọi khi công việc kết thúc

Trong trường hợp này, ứng dụng của bạn sẽ xuất hiện một chuỗi ra thiết bị xuất chuẩn 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[]]
93 như là 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[]]
13 củ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[]]
16 nhưng nó cũng có khả năng gọi một hàm sau khi ngủ kết thúc

Bạn có thể sử dụng chức năng này để cải thiện trải nghiệm người dùng [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[]]
3, bạn có thể làm cho ứng dụng của mình 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 đợi ứng dụng được mở

Ngủ trong wxPython

Có hai sự 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[]]
21 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[]]
22

  1. 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[]]
    
    21 bao gồm rất nhiều widget
  2. 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[]]
    
    21 hướng tới đặc tính đa nền tảng

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[]]
21 không được tích hợp sẵn vào Python, vì vậy bạn phải tự tải chúng về. Nếu bạn không quan tâm 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[]]
21, hãy kiểm tra Cách xây dựng ứng dụng GUI Python với 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[]]
27, 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[]]
28 để thêm lời gọi cho
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

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

Tại đây, bạn thực hiện kế thừa trực tiếp và sau đó gọi 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[]]
28. Hàm này nhận các tham số giống 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[]]
93 của Tkinter

  1. Number millisecond to sleep
  2. Phương thức gọi khi công việc kết thúc

Khi bạn chạy đoạn mã này, bạn sẽ thấy một cửa sổ trắng [trống] xuất hiện mà không có bất kỳ widget nào. Sau 4 giây, bạn sẽ thấy chuỗi "Tôi bị trì hoãn" xuất hiện trong thiết bị xuất chuẩn

Một trong những lợi ích của công 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[]]
28 chính là thread-safe. Bạn có thể sử dụng phương thức này từ một luồng để 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[]]
21

Chủ Đề