Tôi có một chủ đề mở rộng chủ đề. Mã trông hơi như thế này;
class MyThread[Thread]:
def run[self]:
# Do stuff
my_threads = []
while has_jobs[] and len[my_threads] < 5:
new_thread = MyThread[next_job_details[]]
new_thread.run[]
my_threads.append[new_thread]
for my_thread in my_threads
my_thread.join[]
# Do stuff
Vì vậy, ở đây trong mã giả của tôi, tôi kiểm tra xem liệu có bất kỳ công việc nào không [như DB, v.v.] và nếu có một số công việc, và nếu có ít hơn 5 luồng chạy, hãy tạo các luồng mới.
Vì vậy, từ đây, sau đó tôi kiểm tra các chủ đề của mình và đây là nơi tôi bị mắc kẹt, tôi có thể sử dụng .Join [] Sau đó đợi cho đến khi hoàn thành - ngay cả khi các chủ đề khác kết thúc ....
Vì vậy, có một cách để kiểm tra xem một chủ đề được thực hiện, sau đó xóa nó nếu vậy?
ví dụ
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
Cải thiện bài viết
Lưu bài viết
Vấn đề: Để biết khi nào một chủ đề khởi chạy thực sự bắt đầu chạy. To know when will a launched thread actually starts running.
Một tính năng chính của các chủ đề là chúng thực thi độc lập và không theo chủ nghĩa. Điều này có thể đưa ra một vấn đề đồng bộ hóa khó khăn nếu các chủ đề khác trong chương trình cần biết liệu một luồng đã đạt đến một điểm nhất định trong việc thực hiện trước khi thực hiện các hoạt động tiếp theo. Để giải quyết các vấn đề như vậy, hãy sử dụng đối tượng sự kiện từ thư viện luồng.threading library.
Các trường hợp sự kiện tương tự như cờ dính của người Viking cho phép các chủ đề chờ đợi điều gì đó xảy ra. Ban đầu, một sự kiện được đặt thành 0. Nếu sự kiện không được đặt và một luồng chờ đợi sự kiện, nó sẽ chặn [tức là, đi ngủ] cho đến khi sự kiện được đặt. Một chủ đề đặt sự kiện sẽ đánh thức tất cả các luồng xảy ra đang chờ [nếu có]. Nếu một chủ đề chờ đợi một sự kiện đã được thiết lập, nó chỉ tiếp tục, tiếp tục thực hiện.
Mã số 1: Mã sử dụng một sự kiện để phối hợp khởi động một luồng.
from
threading
import
Thread, Event
import
time
def
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
0for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
4for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
5for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
7for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
8for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
9for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1Working 0 Working 11
Working 0 Working 12
Working 0 Working 13
Working 0 Working 14
Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3Working 0 Working 18
Working 0 Working 19
Working 0 Working 15
from
1from
2from
3 from
4Working 0 Working 15
from
6from
7for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
5from
9from
3 threading
1
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3threading
4for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
5threading
6from
3 threading
8from
3 import
0from
3
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3import
3import
4import
5
import
6
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3import
9for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
5Khi chạy mã ở trên, thông báo đếm ngược của Google sẽ luôn xuất hiện sau khi tin nhắn bắt đầu đếm ngược. Điều này được phối hợp bởi sự kiện làm cho luồng chính đợi cho đến khi hàm Thread, Event
1 lần đầu tiên in tin nhắn khởi động.
- Đối tượng sự kiện được sử dụng tốt nhất cho các sự kiện một lần. Đó là, tạo một sự kiện, các luồng chờ sự kiện được thiết lập và sau khi được đặt, sự kiện sẽ bị loại bỏ.
- Mặc dù có thể xóa một sự kiện bằng phương pháp
Thread, Event
2 của nó, nhưng việc xóa một sự kiện một cách an toàn và chờ đợi nó được đặt lại là khó khăn để phối hợp và có thể dẫn đến các sự kiện bị bỏ lỡ, bế tắc hoặc các vấn đề khác [đặc biệt, nó không thể được đảm bảo rằng yêu cầu xóa một sự kiện sau khi đặt nó sẽ thực hiện trước khi một luồng được phát hành trở lại để chờ lại sự kiện]. - Nếu một luồng sẽ liên tục báo hiệu một sự kiện nhiều lần, thì có lẽ tốt hơn là sử dụng một đối tượng điều kiện thay thế.
Mã số 2: Thực hiện bộ hẹn giờ định kỳ mà các luồng khác có thể theo dõi để xem bất cứ khi nào bộ hẹn giờ hết hạn.
import
Thread, Event
4
import
time
def
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
0for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1Working 0 Working 11
Working 0 Working 12
Working 0 Working 13
Working 0 Working 14
Working 0 Working 15
from
1from
2from
3 from
4Working 0 Working 15
from
6from
7for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
5from
9from
3 threading
1
threading
6from
3 threading
8from
3 import
0from
3
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3import
3import
4Khi chạy mã ở trên, thông báo đếm ngược của Google sẽ luôn xuất hiện sau khi tin nhắn bắt đầu đếm ngược. Điều này được phối hợp bởi sự kiện làm cho luồng chính đợi cho đến khi hàm Thread, Event
1 lần đầu tiên in tin nhắn khởi động.
Đối tượng sự kiện được sử dụng tốt nhất cho các sự kiện một lần. Đó là, tạo một sự kiện, các luồng chờ sự kiện được thiết lập và sau khi được đặt, sự kiện sẽ bị loại bỏ.
Working 0 Working 15
import
5Mặc dù có thể xóa một sự kiện bằng phương pháp Thread, Event
2 của nó, nhưng việc xóa một sự kiện một cách an toàn và chờ đợi nó được đặt lại là khó khăn để phối hợp và có thể dẫn đến các sự kiện bị bỏ lỡ, bế tắc hoặc các vấn đề khác [đặc biệt, nó không thể được đảm bảo rằng yêu cầu xóa một sự kiện sau khi đặt nó sẽ thực hiện trước khi một luồng được phát hành trở lại để chờ lại sự kiện].
Nếu một luồng sẽ liên tục báo hiệu một sự kiện nhiều lần, thì có lẽ tốt hơn là sử dụng một đối tượng điều kiện thay thế.
Working 0 Working 15
from
6import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
18Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
20import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
22Mã số 2: Thực hiện bộ hẹn giờ định kỳ mà các luồng khác có thể theo dõi để xem bất cứ khi nào bộ hẹn giờ hết hạn.
import
Thread, Event
4
Thread, Event
7 Thread, Event
8
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
20import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
22for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1def
import
1______72Working 0 Working 15
import
2import
6from
3 import
8for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
23import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
53Working 0 Working 15
import
2time
1from
3 Working 0 Working 13
Code #3 : Use of timer
Working 0 Working 15
import
2time
6from
3 time
8for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
59for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1def
def
1import
2def
3Working 0 Working 15
threading
6from
3 def
7from
3 import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
00Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
68Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
022 for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
04def
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
08import
2def
3for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1Working 0 Working 11
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
04Working 0 Working 14
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
23import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
25from
3 from
4for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
23import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
30Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
68Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
93Working 0 Working 19
def
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
32______72Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
40from
3 import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
43Working 0 Working 15
Working 0 Working 11
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
40from
3from
3 import
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
50& NBSP; Mã số 3: Sử dụng hẹn giờ
Code #4 : Code involving semaphores
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
54from
3 for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
56from
7for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
5for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1Working 0 Working 117
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3Working 0 Working 121
Working 0 Working 19
def
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
61for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1Working 0 Working 11
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
64Working 0 Working 13
Working 0 Working 14
Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
2for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
3Working 0 Working 18
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
73Working 0 Working 15
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
75from
2from
3 from
4for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
1import
5def
for my_thread in my_threads:
if my_thread.done[]:
# process results
del [my_threads[my_thread]] ?? will that work...
80Code #5 :
Working 0 Working 146
Working 0 Working 146
Đầu ra:
Working 0 Working 1
Viết mã liên quan đến rất nhiều đồng bộ hóa khó khăn giữa các luồng có khả năng làm cho đầu của bạn phát nổ.Một cách tiếp cận lành mạnh hơn là các luồng chủ đề như các nhiệm vụ truyền đạt bằng cách sử dụng hàng đợi hoặc làm diễn viên.