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 ]

Chủ Đề