Chủ đề chạy python

Mô-đun này xây dựng các giao diện phân luồng cấp cao hơn trên mô-đun

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
14 cấp thấp hơn. Xem thêm mô-đun
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
15 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
16

Mô-đun

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
0 được cung cấp cho các trường hợp không thể sử dụng
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
17 vì thiếu
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
14

Ghi chú

Bắt đầu với Python 2. 6, mô-đun này cung cấp các bí danh và thuộc tính tuân thủ PEP 8 để thay thế các tên

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
3 được lấy cảm hứng từ API phân luồng của Java. API cập nhật này tương thích với API của mô-đun
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
4. Tuy nhiên, không có lịch trình nào được đặt cho việc ngừng sử dụng tên
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
3 và chúng vẫn được hỗ trợ đầy đủ trong cả Python 2. x và 3. x

Ghi chú

Bắt đầu với Python 2. 5, một số phương thức Thread tăng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 thay vì
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
7 nếu được gọi sai

Chi tiết triển khai CPython. Trong CPython, do Khóa phiên dịch toàn cầu , nên chỉ một luồng có thể thực thi mã Python cùng một lúc (mặc dù một số thư viện định hướng hiệu suất nhất định có thể vượt qua giới hạn này). Nếu bạn muốn ứng dụng của mình tận dụng tốt hơn tài nguyên tính toán của các máy đa lõi, bạn nên sử dụng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
4. Tuy nhiên, phân luồng vẫn là một mô hình thích hợp nếu bạn muốn chạy đồng thời nhiều tác vụ liên kết I/O.

Mô-đun này xác định các chức năng và đối tượng sau

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
0 ()
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
2 ()

Trả về số đối tượng

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
3 hiện đang sống. Số lượng được trả về bằng với độ dài của danh sách được trả về bởi
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
4

Đã thay đổi trong phiên bản 2. 6. Đã thêm chính tả

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
5.

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
7 ()

Một hàm xuất xưởng trả về một đối tượng biến điều kiện mới. Một biến điều kiện cho phép một hoặc nhiều luồng đợi cho đến khi chúng được thông báo bởi một luồng khác

Xem Đối tượng điều kiện .

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
9 ()
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
91 ()

Trả về đối tượng

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
3 hiện tại, tương ứng với luồng điều khiển của người gọi. Nếu luồng điều khiển của người gọi không được tạo thông qua mô-đun
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
17, một đối tượng luồng giả với chức năng hạn chế sẽ được trả về

Đã thay đổi trong phiên bản 2. 6. Đã thêm chính tả

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94.

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
96 ()

Trả về danh sách tất cả các đối tượng

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
3 hiện đang sống. Danh sách này bao gồm các luồng daemon, các đối tượng luồng giả được tạo bởi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 và luồng chính. Nó loại trừ các chủ đề đã kết thúc và các chủ đề chưa được bắt đầu

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
70 ()

Hàm xuất xưởng trả về một đối tượng sự kiện mới. Một sự kiện quản lý một cờ có thể được đặt thành true bằng phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
71 và đặt lại thành false bằng phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
72. Phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
73 chặn cho đến khi cờ là true

Xem Đối tượng sự kiện .

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
75¶

Một lớp đại diện cho dữ liệu cục bộ luồng. Dữ liệu luồng cục bộ là dữ liệu có giá trị cụ thể theo luồng. Để quản lý dữ liệu cục bộ của luồng, chỉ cần tạo một phiên bản của

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
76 (hoặc một lớp con) và lưu trữ các thuộc tính trên đó

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
4

Các giá trị của cá thể sẽ khác nhau đối với các luồng riêng biệt

Để biết thêm chi tiết và các ví dụ mở rộng, hãy xem chuỗi tài liệu của mô-đun

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
77

Mới trong phiên bản 2. 4

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
79 ()

Hàm xuất xưởng trả về một đối tượng khóa nguyên thủy mới. Khi một luồng đã có được nó, các nỗ lực tiếp theo để có được nó sẽ bị chặn cho đến khi nó được giải phóng;

Xem Khóa đối tượng .

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
91 ()

Một chức năng xuất xưởng trả về một đối tượng khóa reentrant mới. Một khóa reentrant phải được phát hành bởi chủ đề có được nó. Khi một luồng đã nhận được khóa truy cập lại, luồng đó có thể lấy lại khóa đó mà không bị chặn;

Xem Đối tượng RLock .

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
93 ([ giá trị ])

Hàm xuất xưởng trả về một đối tượng semaphore mới. Một semaphore quản lý một bộ đếm đại diện cho số lượng cuộc gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 trừ đi số lượng cuộc gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95, cộng với một giá trị ban đầu. Phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 chặn nếu cần cho đến khi nó có thể trả về mà không làm cho bộ đếm âm. Nếu không được cung cấp, giá trị mặc định là 1

Xem Đối tượng Semaphore .

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
98 ([ giá trị ])

Một hàm xuất xưởng trả về một đối tượng semaphore giới hạn mới. Semaphore có giới hạn kiểm tra để đảm bảo giá trị hiện tại của nó không vượt quá giá trị ban đầu. Nếu có,

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
99 được nâng lên. Trong hầu hết các tình huống, đèn hiệu được sử dụng để bảo vệ các tài nguyên có dung lượng hạn chế. Nếu semaphore được phát hành quá nhiều lần, đó là dấu hiệu của một lỗi. Nếu không được cung cấp, giá trị mặc định là 1

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9____2171

Một lớp đại diện cho một luồng điều khiển. Lớp này có thể được phân lớp một cách an toàn theo một cách hạn chế

Xem Đối tượng luồng .

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9____2173

Một luồng thực thi một chức năng sau một khoảng thời gian xác định đã trôi qua

Xem Đối tượng hẹn giờ .

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
175 ( chức năng )

Đặt chức năng theo dõi cho tất cả các luồng bắt đầu từ mô-đun

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
17. Func sẽ được chuyển đến
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
177 cho mỗi luồng, trước khi phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 của nó được gọi

Mới trong phiên bản 2. 3

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
140 ( chức năng )

Đặt chức năng hồ sơ cho tất cả các luồng bắt đầu từ mô-đun

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
17. Func sẽ được chuyển đến
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
142 cho mỗi luồng, trước khi phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 của nó được gọi

Mới trong phiên bản 2. 3

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
145 ([ kích thước ])

Trả về kích thước ngăn xếp luồng được sử dụng khi tạo luồng mới. Đối số kích thước tùy chọn chỉ định kích thước ngăn xếp sẽ được sử dụng cho các chuỗi được tạo sau đó và phải là 0 (sử dụng nền tảng hoặc mặc định được định cấu hình) hoặc giá trị số nguyên dương ít nhất là 32.768 (32 KiB). Nếu kích thước không được chỉ định, 0 được sử dụng. Nếu việc thay đổi kích thước ngăn xếp luồng không được hỗ trợ, thì một

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
146 sẽ xuất hiện. Nếu kích thước ngăn xếp được chỉ định không hợp lệ, một
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
99 sẽ được nâng lên và kích thước ngăn xếp không được sửa đổi. 32kB hiện là giá trị kích thước ngăn xếp tối thiểu được hỗ trợ để đảm bảo đủ không gian ngăn xếp cho chính trình thông dịch. Lưu ý rằng một số nền tảng có thể có các hạn chế cụ thể đối với các giá trị đối với kích thước ngăn xếp, chẳng hạn như yêu cầu kích thước ngăn xếp tối thiểu > 32kB hoặc yêu cầu phân bổ theo bội số của kích thước trang bộ nhớ hệ thống - nên tham khảo tài liệu nền tảng để biết thêm thông tin (trang 4kB là phổ biến . khả dụng. Windows, hệ thống có chủ đề POSIX

Mới trong phiên bản 2. 5

ngoại lệ
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
149¶

Tăng lên cho các lỗi liên quan đến luồng khác nhau như được mô tả bên dưới. Lưu ý rằng nhiều giao diện sử dụng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 thay vì
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
146

Giao diện chi tiết cho các đối tượng được ghi dưới đây

Thiết kế của mô-đun này dựa trên mô hình phân luồng của Java. Tuy nhiên, trong đó Java tạo các khóa và biến điều kiện hành vi cơ bản của mọi đối tượng, thì chúng là các đối tượng riêng biệt trong Python. Lớp

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
3 của Python hỗ trợ một tập hợp con hành vi của lớp Thread của Java; . Các phương thức tĩnh của lớp Thread của Java, khi được triển khai, được ánh xạ tới các hàm cấp mô-đun

Tất cả các phương pháp được mô tả dưới đây được thực thi nguyên tử

16. 2. 1. Đối tượng luồng¶

Lớp này đại diện cho một hoạt động được chạy trong một luồng điều khiển riêng biệt. Có hai cách để xác định hoạt động. bằng cách chuyển một đối tượng có thể gọi được đến hàm tạo hoặc bằng cách ghi đè phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 trong một lớp con. Không có phương thức nào khác (ngoại trừ hàm tạo) được ghi đè trong một lớp con. Nói cách khác, chỉ ghi đè các phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
154 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 của lớp này

Khi một đối tượng luồng được tạo, hoạt động của nó phải được bắt đầu bằng cách gọi phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
156 của luồng. Điều này gọi phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 trong một luồng điều khiển riêng biệt

Khi hoạt động của chuỗi được bắt đầu, chuỗi được coi là 'còn sống'. Nó ngừng hoạt động khi phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 của nó chấm dứt - thông thường hoặc bằng cách đưa ra một ngoại lệ chưa được xử lý. Phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
159 kiểm tra xem luồng còn sống hay không

Các luồng khác có thể gọi phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 của một luồng. Điều này chặn luồng đang gọi cho đến khi luồng có phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 được gọi bị chấm dứt

Một chủ đề có một tên. Tên có thể được chuyển đến hàm tạo và đọc hoặc thay đổi thông qua thuộc tính

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
162

Một luồng có thể được gắn cờ là một “luồng daemon”. Ý nghĩa của cờ này là toàn bộ chương trình Python sẽ thoát khi chỉ còn lại các luồng daemon. Giá trị ban đầu được kế thừa từ chuỗi tạo. Cờ có thể được đặt thông qua thuộc tính

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
163

Ghi chú

Chủ đề daemon bị dừng đột ngột khi tắt máy. Tài nguyên của họ (chẳng hạn như tệp đang mở, giao dịch cơ sở dữ liệu, v.v. ) có thể không được phát hành đúng cách. Nếu bạn muốn các chuỗi của mình dừng một cách nhẹ nhàng, hãy làm cho chúng không phải daemon và sử dụng cơ chế báo hiệu phù hợp, chẳng hạn như một

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
164

Có một đối tượng "chủ đề chính"; . Nó không phải là một chủ đề daemon

Có khả năng "đối tượng chuỗi giả" được tạo. Đây là các đối tượng luồng tương ứng với “luồng lạ”, là luồng điều khiển bắt đầu bên ngoài mô-đun luồng, chẳng hạn như trực tiếp từ mã C. Các đối tượng luồng giả có chức năng hạn chế; . Chúng không bao giờ bị xóa, vì không thể phát hiện ra sự chấm dứt của các luồng ngoài hành tinh

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9_______2171 ( group=None, target=None, name=None, args=(), kwargs={} )

Hàm tạo này phải luôn được gọi với các đối số từ khóa. Đối số là

nhóm phải là

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168;

target là đối tượng có thể gọi được gọi bằng phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178. Mặc định là
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168, nghĩa là không có gì được gọi

tên là tên chủ đề. Theo mặc định, một tên duy nhất được tạo ở dạng “Thread-N” trong đó N là một số thập phân nhỏ

args là bộ đối số cho lệnh gọi đích. Mặc định là

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
02

kwargs là một từ điển các đối số từ khóa cho lệnh gọi đích. Mặc định là

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
03

Nếu lớp con ghi đè hàm tạo, nó phải đảm bảo gọi hàm tạo của lớp cơ sở (

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
04) trước khi thực hiện bất kỳ điều gì khác đối với luồng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
05 ()

Bắt đầu hoạt động của chủ đề

Nó phải được gọi nhiều nhất một lần cho mỗi đối tượng luồng. Nó sắp xếp để gọi phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 của đối tượng trong một luồng điều khiển riêng biệt

Phương thức này sẽ tăng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 nếu được gọi nhiều lần trên cùng một đối tượng luồng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
08 ()

Phương thức đại diện cho hoạt động của luồng

Bạn có thể ghi đè phương thức này trong một lớp con. Phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 tiêu chuẩn gọi đối tượng có thể gọi được chuyển đến hàm tạo của đối tượng làm đối số đích, nếu có, với các đối số tuần tự và từ khóa được lấy từ các đối số args và kwargs tương ứng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
10 ([ hết thời gian ])

Chờ cho đến khi chủ đề kết thúc. Điều này chặn luồng đang gọi cho đến khi luồng có phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 được gọi kết thúc – thông thường hoặc thông qua một ngoại lệ chưa được xử lý – hoặc cho đến khi hết thời gian chờ tùy chọn xảy ra

Khi đối số hết thời gian chờ xuất hiện chứ không phải là

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168, thì đó phải là số dấu phẩy động chỉ định thời gian chờ cho thao tác tính bằng giây (hoặc phân số của nó). Vì
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 luôn trả về
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168, bạn phải gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
15 sau
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 để quyết định xem có xảy ra thời gian chờ hay không – nếu chuỗi vẫn còn hoạt động, cuộc gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 đã hết thời gian chờ

Khi không có đối số thời gian chờ hoặc

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168, hoạt động sẽ bị chặn cho đến khi luồng kết thúc

Một chủ đề có thể được

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160ed nhiều lần

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 tăng
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 nếu cố gắng tham gia chuỗi hiện tại vì điều đó sẽ gây ra bế tắc. Đó cũng là một lỗi đối với
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
160 một luồng trước khi nó được bắt đầu và cố gắng làm như vậy sẽ gây ra ngoại lệ tương tự

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
23¶

Một chuỗi chỉ được sử dụng cho mục đích nhận dạng. Nó không có ngữ nghĩa. Nhiều chủ đề có thể được đặt tên giống nhau. Tên ban đầu được đặt bởi hàm tạo

Mới trong phiên bản 2. 6

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
24 ()
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
25 ()

Pre-2. 6 API cho

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
162

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
27¶

'Số nhận dạng chủ đề' của chủ đề này hoặc

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168 nếu chủ đề chưa được bắt đầu. Đây là một số nguyên khác không. Xem chức năng
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
29. Số nhận dạng luồng có thể được tái chế khi một luồng thoát và một luồng khác được tạo. Mã định danh khả dụng ngay cả sau khi chuỗi đã thoát

Mới trong phiên bản 2. 6

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
30 ()
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
31 ()

Trả về xem thread còn sống hay không

Phương thức này trả về

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
32 ngay trước khi phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 bắt đầu cho đến ngay sau khi phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
178 kết thúc. Hàm mô-đun
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
4 trả về danh sách tất cả các luồng đang hoạt động

Đã thay đổi trong phiên bản 2. 6. Đã thêm chính tả

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
159.

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
37¶

Một giá trị boolean cho biết luồng này có phải là luồng daemon (True) hay không (False). Điều này phải được thiết lập trước khi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
156 được gọi, nếu không thì
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 được nâng lên. Giá trị ban đầu của nó được kế thừa từ chuỗi tạo;

Toàn bộ chương trình Python thoát khi không còn luồng không phải daemon nào còn sống

Mới trong phiên bản 2. 6

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
42 ()
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
43 ()

Pre-2. 6 API cho

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
163

16. 2. 2. Khóa đối tượng¶

Khóa nguyên thủy là nguyên thủy đồng bộ hóa không thuộc sở hữu của một luồng cụ thể khi bị khóa. Trong Python, nó hiện là nguyên mẫu đồng bộ hóa mức thấp nhất hiện có, được triển khai trực tiếp bởi mô-đun mở rộng

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
14

Khóa nguyên thủy ở một trong hai trạng thái, "đã khóa" hoặc "đã mở khóa". Nó được tạo ở trạng thái mở khóa. Nó có hai phương thức cơ bản,

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94. Khi trạng thái được mở khóa,
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 sẽ thay đổi trạng thái thành bị khóa và quay lại ngay lập tức. Khi trạng thái bị khóa,
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 chặn cho đến khi một cuộc gọi đến
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 trong một luồng khác thay đổi nó thành không khóa, sau đó cuộc gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 đặt lại thành bị khóa và trả về. Phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 chỉ nên được gọi ở trạng thái bị khóa; . Nếu một nỗ lực được thực hiện để giải phóng một ổ khóa không khóa, một
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
146 sẽ được nâng lên

Khi có nhiều hơn một luồng bị chặn trong

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 đang chờ trạng thái chuyển sang mở khóa, chỉ một luồng tiếp tục khi lệnh gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 đặt lại trạng thái thành mở khóa;

Tất cả các phương pháp được thực hiện nguyên tử

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
56_______057 ([ chặn ])

Nhận khóa, chặn hoặc không chặn

Khi được gọi với đối số chặn được đặt thành

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
32 (mặc định), hãy chặn cho đến khi khóa được mở khóa, sau đó đặt thành bị khóa và trả về
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
32

Khi được gọi với đối số chặn được đặt thành

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
41, không chặn. Nếu một cuộc gọi có chặn được đặt thành
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
32 sẽ chặn, hãy trả lại
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
41 ngay lập tức;

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
56_______065 ()

Phát hành một khóa

Khi khóa bị khóa, đặt lại thành mở khóa và quay lại. Nếu bất kỳ chủ đề nào khác bị chặn đang chờ khóa được mở khóa, hãy cho phép chính xác một trong số chúng tiếp tục

Khi được gọi trên khóa đã mở, một

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
146 được nâng lên

không có giá trị quay lại

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
67 ()
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
68

16. 2. 3. RLock Đối tượng¶

Khóa reentrant là nguyên thủy đồng bộ hóa có thể được mua lại nhiều lần bởi cùng một luồng. Trong nội bộ, nó sử dụng các khái niệm về “chủ đề sở hữu” và “cấp độ đệ quy” ngoài trạng thái khóa/mở khóa được sử dụng bởi các khóa nguyên thủy. Ở trạng thái bị khóa, một số luồng sở hữu khóa;

Để khóa khóa, một luồng gọi phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 của nó; . Để mở khóa, một luồng gọi phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 của nó. Các cặp cuộc gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95/
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 có thể được lồng vào nhau;

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
76
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
57 ([ blocking=1 ])

Nhận khóa, chặn hoặc không chặn

Khi được gọi mà không có đối số. nếu chủ đề này đã sở hữu khóa, hãy tăng mức đệ quy lên một và quay lại ngay lập tức. Mặt khác, nếu một chủ đề khác sở hữu khóa, hãy chặn cho đến khi khóa được mở khóa. Khi khóa được mở khóa (không thuộc sở hữu của bất kỳ luồng nào), sau đó lấy quyền sở hữu, đặt mức đệ quy thành một và quay lại. Nếu có nhiều luồng bị chặn chờ cho đến khi khóa được mở khóa, thì mỗi lần chỉ một luồng có thể giành quyền sở hữu khóa. Không có giá trị trả lại trong trường hợp này

Khi được gọi với đối số chặn được đặt thành true, hãy thực hiện tương tự như khi được gọi mà không có đối số và trả về true

Khi được gọi với đối số chặn được đặt thành sai, không chặn. Nếu một cuộc gọi không có đối số sẽ bị chặn, hãy trả về false ngay lập tức;

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
76
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
65 ()

Phát hành khóa, giảm mức đệ quy. Nếu sau khi giảm, nó bằng 0, hãy đặt lại khóa thành mở khóa (không thuộc sở hữu của bất kỳ chuỗi nào) và nếu bất kỳ chuỗi nào khác bị chặn đang chờ khóa được mở khóa, hãy cho phép chính xác một trong số chúng tiếp tục. Nếu sau khi giảm, mức đệ quy vẫn khác không, thì khóa vẫn bị khóa và thuộc sở hữu của luồng gọi

Chỉ gọi phương thức này khi luồng gọi sở hữu khóa. Một

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 được nâng lên nếu phương thức này được gọi khi khóa được mở khóa

không có giá trị quay lại

16. 2. 4. Đối tượng điều kiện¶

Một biến điều kiện luôn được liên kết với một số loại khóa; . (Chuyển một vào rất hữu ích khi một số biến điều kiện phải chia sẻ cùng một khóa. )

Một biến điều kiện có các phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 gọi các phương thức tương ứng của khóa được liên kết. Nó cũng có phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
73 và phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
85. Ba cái này chỉ được gọi khi luồng gọi đã có được khóa, nếu không, một
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 sẽ được nâng lên

Phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
73 giải phóng khóa, sau đó chặn cho đến khi nó được đánh thức bằng lệnh gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84 hoặc
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
85 cho cùng một biến điều kiện trong một luồng khác. Sau khi được đánh thức, nó lấy lại khóa và quay trở lại. Cũng có thể chỉ định thời gian chờ

Phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84 đánh thức một trong các luồng đang chờ biến điều kiện, nếu có đang chờ. Phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
85 đánh thức tất cả các luồng đang chờ biến điều kiện

Ghi chú. các phương pháp

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
85 không giải phóng khóa;

Mẹo. phong cách lập trình điển hình sử dụng các biến điều kiện sử dụng khóa để đồng bộ hóa quyền truy cập vào một số trạng thái được chia sẻ; . Ví dụ: đoạn mã sau là tình huống chung giữa nhà sản xuất và người tiêu dùng với dung lượng bộ đệm không giới hạn

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()

Để chọn giữa

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
85, hãy xem xét liệu một thay đổi trạng thái có thể thú vị đối với chỉ một hoặc một số luồng đang chờ hay không. e. g. trong một tình huống điển hình của nhà sản xuất-người tiêu dùng, việc thêm một mục vào bộ đệm chỉ cần đánh thức một luồng người tiêu dùng

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9_______87 ([ khóa ])

Nếu đối số khóa được cung cấp và không phải là

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168, thì đối số đó phải là đối tượng
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
05 hoặc
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
06 và được sử dụng làm khóa cơ bản. Mặt khác, một đối tượng
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
06 mới được tạo và sử dụng làm khóa bên dưới

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
57 ( *args )

Có được khóa cơ bản. Phương thức này gọi phương thức tương ứng trên khóa bên dưới;

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
65 ()

Thả khóa bên dưới. Phương thức này gọi phương thức tương ứng trên khóa bên dưới;

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
10 ([ hết thời gian ])

Đợi cho đến khi được thông báo hoặc cho đến khi hết thời gian chờ. Nếu luồng đang gọi chưa nhận được khóa khi phương thức này được gọi, một

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 sẽ được nâng lên

Phương pháp này giải phóng khóa cơ bản, sau đó chặn cho đến khi nó được đánh thức bằng lệnh gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84 hoặc
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
85 cho cùng một biến điều kiện trong một luồng khác hoặc cho đến khi hết thời gian chờ tùy chọn xảy ra. Sau khi được đánh thức hoặc hết thời gian chờ, nó sẽ lấy lại khóa và quay lại

Khi đối số hết thời gian chờ xuất hiện chứ không phải là

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168, thì đó phải là số dấu phẩy động chỉ định thời gian chờ cho thao tác tính bằng giây (hoặc phân số của nó)

Khi khóa cơ bản là một

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
06, nó sẽ không được giải phóng bằng phương pháp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 của nó, vì điều này có thể không thực sự mở khóa khi nó được mua lại nhiều lần theo cách đệ quy. Thay vào đó, một giao diện bên trong của lớp
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
06 được sử dụng, giao diện này thực sự mở khóa nó ngay cả khi nó đã được thu thập một cách đệ quy nhiều lần. Sau đó, một giao diện nội bộ khác được sử dụng để khôi phục mức đệ quy khi khóa được lấy lại

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
18 ( n=1 ) ¶Theo mặc định, đánh thức một luồng đang chờ trong điều kiện này, nếu có. Nếu luồng đang gọi chưa nhận được khóa khi phương thức này được gọi, một
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 sẽ được nâng lên

Phương thức này đánh thức tối đa n luồng đang chờ biến điều kiện;

Việc triển khai hiện tại đánh thức đúng n luồng, nếu có ít nhất n luồng đang chờ. Tuy nhiên, không an toàn khi dựa vào hành vi này. Việc triển khai được tối ưu hóa trong tương lai đôi khi có thể đánh thức hơn n luồng

Ghi chú. một luồng được đánh thức không thực sự quay trở lại từ cuộc gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
73 của nó cho đến khi nó có thể lấy lại khóa. Vì
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84 không nhả khóa nên người gọi nó phải

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
22 ()
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
23 ()

Đánh thức tất cả các chủ đề đang chờ trong điều kiện này. Phương pháp này hoạt động giống như

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
84, nhưng đánh thức tất cả các luồng đang chờ thay vì một. Nếu luồng đang gọi chưa nhận được khóa khi phương thức này được gọi, một
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
6 sẽ được nâng lên

Đã thay đổi trong phiên bản 2. 6. Đã thêm chính tả

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
26.

16. 2. 5. Đối tượng Semaphore¶

Đây là một trong những nguyên tắc đồng bộ hóa lâu đời nhất trong lịch sử khoa học máy tính, được phát minh bởi nhà khoa học máy tính đầu tiên người Hà Lan Edsger W. Dijkstra (anh ấy đã sử dụng

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
27 và
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
28 thay vì
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94)

Một semaphore quản lý một bộ đếm nội bộ được giảm dần theo mỗi cuộc gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 và tăng lên theo mỗi cuộc gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94. Bộ đếm không bao giờ có thể xuống dưới 0;

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9_______393 ([ giá trị ])

Đối số tùy chọn đưa ra giá trị ban đầu cho bộ đếm nội bộ; . Nếu giá trị đã cho nhỏ hơn 0, thì

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
99 được nâng lên

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
57 ([ chặn ])

Có được một semaphore

Khi được gọi mà không có đối số. nếu bộ đếm bên trong lớn hơn 0 khi nhập, hãy giảm nó đi một và quay lại ngay lập tức. Nếu nó là 0 trên mục nhập, hãy chặn, đợi cho đến khi một số chuỗi khác đã gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 để làm cho nó lớn hơn 0. Điều này được thực hiện với khóa liên động thích hợp để nếu nhiều cuộc gọi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 bị chặn, thì
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 sẽ đánh thức chính xác một trong số chúng. Việc triển khai có thể chọn một cách ngẫu nhiên, vì vậy không nên dựa vào thứ tự đánh thức các luồng bị chặn. Không có giá trị trả lại trong trường hợp này

Khi được gọi với lệnh chặn được đặt thành true, hãy thực hiện tương tự như khi được gọi mà không có đối số và trả về giá trị true

Khi được gọi với tính năng chặn được đặt thành false, không chặn. Nếu một cuộc gọi không có đối số sẽ bị chặn, hãy trả về false ngay lập tức;

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
65 ()

Phát hành một semaphore, tăng bộ đếm nội bộ lên một. Khi nó bằng 0 trên mục nhập và một luồng khác đang chờ nó trở lại lớn hơn 0 lần nữa, hãy đánh thức luồng đó

16. 2. 5. 1. ______844 Ví dụ¶

Semaphores thường được sử dụng để bảo vệ các tài nguyên có dung lượng hạn chế, chẳng hạn như máy chủ cơ sở dữ liệu. Trong bất kỳ tình huống nào mà kích thước của tài nguyên là cố định, bạn nên sử dụng một semaphore có giới hạn. Trước khi sinh ra bất kỳ luồng worker nào, luồng chính của bạn sẽ khởi tạo semaphore

________số 8

Sau khi được sinh ra, worker thread gọi các phương thức thu thập và giải phóng của semaphore khi chúng cần kết nối với máy chủ

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9

Việc sử dụng semaphore có giới hạn giúp giảm khả năng lỗi lập trình khiến semaphore được giải phóng nhiều hơn mức thu được sẽ không bị phát hiện

16. 2. 6. Đối tượng sự kiện¶

Đây là một trong những cơ chế đơn giản nhất để giao tiếp giữa các luồng. một luồng báo hiệu một sự kiện và các luồng khác chờ đợi nó

Một đối tượng sự kiện quản lý một cờ nội bộ có thể được đặt thành true bằng phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
71 và đặt lại thành false bằng phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
72. Phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
73 chặn cho đến khi cờ là true

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
70¶

Cờ nội bộ ban đầu là sai

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
50 ()
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
51 ()

Trả về true khi và chỉ khi cờ nội bộ là true

Đã thay đổi trong phiên bản 2. 6. Đã thêm chính tả

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
52.

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
53 ()

Đặt cờ nội bộ thành true. Tất cả các chủ đề đang chờ nó trở thành sự thật được đánh thức. Các chủ đề gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
73 sau khi cờ là đúng sẽ hoàn toàn không bị chặn

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
55 ()

Đặt lại cờ nội bộ thành sai. Sau đó, các chủ đề gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
73 sẽ chặn cho đến khi
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
71 được gọi để đặt lại cờ nội bộ thành true

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
10 ([ hết thời gian ])

Chặn cho đến khi cờ nội bộ là true. Nếu cờ nội bộ là đúng khi vào, hãy quay lại ngay lập tức. Nếu không, hãy chặn cho đến khi một chuỗi khác gọi

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
71 để đặt cờ thành true hoặc cho đến khi hết thời gian chờ tùy chọn xảy ra

Khi đối số hết thời gian chờ xuất hiện chứ không phải là

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168, thì đó phải là số dấu phẩy động chỉ định thời gian chờ cho thao tác tính bằng giây (hoặc phân số của nó)

Phương thức này trả về cờ nội bộ khi thoát, do đó, nó sẽ luôn trả về

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
32 trừ khi hết thời gian chờ và thao tác hết thời gian chờ

Đã thay đổi trong phiên bản 2. 7. Trước đây, phương thức luôn trả về

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
168.

16. 2. 7. Đối tượng hẹn giờ¶

Lớp này đại diện cho một hành động chỉ được chạy sau một khoảng thời gian nhất định - một bộ đếm thời gian.

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
63 là một lớp con của
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
3 và như vậy cũng có chức năng như một ví dụ về tạo chủ đề tùy chỉnh

Bộ hẹn giờ được bắt đầu, như với các luồng, bằng cách gọi phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
156 của chúng. Có thể dừng bộ đếm thời gian (trước khi hành động của nó bắt đầu) bằng cách gọi phương thức
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
66. Khoảng thời gian mà bộ hẹn giờ sẽ đợi trước khi thực hiện hành động của nó có thể không hoàn toàn giống với khoảng thời gian do người dùng chỉ định

Ví dụ

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
7

lớp
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9_______2173 ( khoảng, hàm, args=[], kwargs={} )<

Tạo một bộ đếm thời gian sẽ chạy chức năng với các đối số args và đối số từ khóa kwargs, sau khi khoảng thời gian giây trôi qua

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
69 ()

Dừng bộ đếm thời gian và hủy bỏ việc thực hiện hành động của bộ đếm thời gian. Điều này sẽ chỉ hoạt động nếu bộ hẹn giờ vẫn đang trong giai đoạn chờ

16. 2. 8. Sử dụng khóa, điều kiện và dấu hiệu trong câu lệnh maxconnections = 5 ... pool_sema = BoundedSemaphore(value=maxconnections) 70¶

Tất cả các đối tượng được cung cấp bởi mô-đun này có các phương thức

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 có thể được sử dụng làm trình quản lý ngữ cảnh cho câu lệnh
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
70. Phương thức
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
95 sẽ được gọi khi khối được nhập và
# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
94 sẽ được gọi khi khối được thoát

Hiện tại, các đối tượng

maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
05,
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
06,
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
78,
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
44 và
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
80 có thể được sử dụng làm trình quản lý ngữ cảnh câu lệnh
maxconnections = 5
...
pool_sema = BoundedSemaphore(value=maxconnections)
70. Ví dụ

# Consume one item
cv.acquire()
while not an_item_is_available():
    cv.wait()
get_an_available_item()
cv.release()

# Produce one item
cv.acquire()
make_an_item_available()
cv.notify()
cv.release()
9

16. 2. 9. Nhập mã theo luồng¶

Mặc dù máy móc nhập khẩu an toàn theo luồng, nhưng có hai hạn chế chính đối với nhập khẩu theo luồng do những hạn chế cố hữu trong cách cung cấp tính năng an toàn luồng

  • Đầu tiên, ngoài mô-đun chính, việc nhập không được có tác dụng phụ là tạo ra một chuỗi mới và sau đó chờ chuỗi đó theo bất kỳ cách nào. Việc không tuân thủ hạn chế này có thể dẫn đến bế tắc nếu luồng được sinh ra trực tiếp hoặc gián tiếp cố gắng nhập mô-đun

  • Thứ hai, tất cả các lần nhập phải được hoàn thành trước khi trình thông dịch bắt đầu tự tắt. Điều này có thể đạt được dễ dàng nhất bằng cách chỉ thực hiện nhập từ các luồng không phải daemon được tạo thông qua mô-đun luồng. Các luồng daemon và các luồng được tạo trực tiếp với mô-đun luồng sẽ yêu cầu một số hình thức đồng bộ hóa khác để đảm bảo chúng không cố gắng nhập sau khi tắt hệ thống đã bắt đầu. Việc không tuân thủ hạn chế này sẽ dẫn đến các trường hợp ngoại lệ không liên tục và sự cố trong khi tắt trình thông dịch (do quá trình nhập muộn cố gắng truy cập máy móc không còn ở trạng thái hợp lệ)