Hướng dẫn how do i know if a python thread is running? - làm cách nào để biết chuỗi python có đang chạy không?

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...
0

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
4
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
5

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
7
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
8
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
9

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
Working 0
Working 1
1
Working 0
Working 1
2
Working 0
Working 1
3
Working 0
Working 1
4

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3
Working 0
Working 1
8
Working 0
Working 1
9

Working 0
Working 1
5from1from2from3 from4

Working 0
Working 1
5from6from7
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
5

from9from3 threading 1

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3threading 4
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
5

threading 6from3 threading 8from3 import0from3

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3import3import4

import5

import6

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3import9
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
5

Khi 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, Event1 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, Event2 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, Event4

import time

def

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
0

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
Working 0
Working 1
1
Working 0
Working 1
2
Working 0
Working 1
3
Working 0
Working 1
4

Working 0
Working 1
5from1from2from3 from4

Working 0
Working 1
5from6from7
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
5

from9from3 threading 1

threading 6from3 threading 8from3 import0from3

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3import3import4

Khi 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, Event1 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 1
5import5

Mặc dù có thể xóa một sự kiện bằng phương pháp Thread, Event2 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 1
5from6import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
18

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
20import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
22

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, Event4

Thread, Event7 Thread, Event8

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
20import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
22

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1def import1______72

Working 0
Working 1
5import2import6from3 import8

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
23import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
53

Working 0
Working 1
5import2time1from3
Working 0
Working 1
3
Code #3 : Use of timer

Working 0
Working 1
5import2time6from3 time8

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
59

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1def def1import2def3

Working 0
Working 1
5threading 6from3 def7from3 import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
00

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
68

Working 0
Working 1
5
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...
04

def

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
08import2def3

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
Working 0
Working 1
1
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
04
Working 0
Working 1
4

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
23import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
25from3 from4

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
23import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
30

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
68

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
93
Working 0
Working 1
9

def

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
32______72

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
40from3 import2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
43

Working 0
Working 1
5
Working 0
Working 1
1
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
40from3from3 import2
for 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...
54from3
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
56from7
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
5

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
Working 0
Working 1
17

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3
Working 0
Working 1
21
Working 0
Working 1
9

def

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
61

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1
Working 0
Working 1
1
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
64
Working 0
Working 1
3
Working 0
Working 1
4

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
2
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
3
Working 0
Working 1
8
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
73

Working 0
Working 1
5
for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
75from2from3 from4

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
1import5

def

for my_thread in my_threads:
    if my_thread.done():
        # process results
        del (my_threads[my_thread]) ?? will that work...
80
 
Code #5 :

Working 0
Working 1
46

Working 0
Working 1
46

Đầ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.


Phương pháp nào được sử dụng để xác định một luồng trong Python?

current_thread (). Nhận dạng (hoặc luồng. currentThread (). Nhận dạng cho Python <2.6). ident (or threading. currentThread(). ident for Python < 2.6).

Python có chạy trên một chủ đề không?

Python không phải là ngôn ngữ đơn.Các quá trình Python thường sử dụng một luồng duy nhất vì GIL.Mặc dù GiL, các thư viện thực hiện các nhiệm vụ nặng về tính toán như Numpy, Scipy và Pytorch sử dụng các triển khai dựa trên C dưới mui xe, cho phép sử dụng nhiều lõi.. Python processes typically use a single thread because of the GIL. Despite the GIL, libraries that perform computationally heavy tasks like numpy, scipy and pytorch utilise C-based implementations under the hood, allowing the use of multiple cores.