Các luồng luôn luôn chạy trong nền cung cấp các hỗ trợ cho các luồng chính hoặc không phải là Daemon, các luồng thực thi nền đó được coi là chủ đề daemon. Chủ đề daemon không chặn luồng chính thoát ra và tiếp tục chạy ở chế độ nền. Bài viết này dựa trên luồng trong Python, ở đây chúng tôi thảo luận về chủ đề daemon với các ví dụ.Daemon Threads. The Daemon Thread does not block the main thread from exiting and continues to run in the background. This article is based on threading in python, here we discuss daemon thread with examples.
Có một trong những ví dụ tốt nhất về chủ đề daemon là Trình thu gom rác vì chúng tôi cho rằng luồng chính đang thực thi hoặc chạy, tại thời điểm đó, bất kỳ vấn đề bộ nhớ nào xảy ra sau đó ngay lập tức máy ảo Python [PVM] sẽ thực hiện bộ sưu tập rác. Bộ sưu tập sẽ thực thi trong nền và phá hủy tất cả các đối tượng vô dụng và sau đó bộ nhớ miễn phí theo mặc định sẽ được cung cấp, một khi có bộ nhớ miễn phí sẽ có sẵn thì luồng chính sẽ được thực thi mà không gặp vấn đề gì.Garbage Collector because we assume that the main thread is executing or running, at that time any memory problem occurs then immediately python virtual machine[PVM] is going to execute Garbage Collector.The Garbage Collector is going to execute in the background and destroy all the useless objects and then free memory by default will be provided, once there is free memory will available then the main thread is going to be executed without any problem.
Chủ đề bình thường học dòng chảy của chủ đề không phải là Daemon
Ví dụ này đơn giản hóa luồng của một luồng không phải là Daemon trong đó chúng tôi đã tạo hàm Tên_1 [] có một số dòng hướng dẫn để thực thi để tiết lộ cách thực hiện luồng không Daemon khi luồng chính kết thúc. Ở tiếp theo, chúng tôi đã tạo luồng t của chức năng Thread_1 [] hiện được coi là một luồng không hoạt động, bây giờ chúng tôi bắt đầu luồng t và chúng tôi cũng đã tạm thời dừng việc thực hiện luồng chính cho 5SECS. Của thời gian, giữa 5 giây này. Chủ đề T tiếp tục thực hiện và khi luồng chính sẽ được thực thi sau 5 giây. Trường hợp nó dừng công việc của nó nhưng có một chủ đề t vẫn đang được thực hiện bởi vì nó là một luồng không phải là Daemon và thực hiện hướng dẫn của họ cho đến khi hoàn thành của họ. & NBSP;thread_1[] name function which having some lines of instructions to execute which reveal how the non-daemon thread is executed when the main thread terminates. At the next we have created the thread T of function thread_1[] which is currently considered as a non-active thread, now we start the thread T, and we also have temporarily stopped the execution of the main thread for 5secs. Of time, between this 5sec. Thread T continues its execution and when the main thread is going to be executed after 5sec. Where it stops its work but there is a thread T still is in execution because it is a non-daemon thread and executes their instruction until their completion.
Dưới đây là việc thực hiện:
Python3
from
threading
import
*
import
this is thread T this is thread T this is Main Thread1
this is thread T this is thread T this is Main Thread2
this is thread T this is thread T this is Main Thread3
this is thread T this is thread T this is Main Thread4
this is thread T this is thread T this is Main Thread5
this is thread T this is thread T this is Main Thread6
this is thread T this is thread T this is Main Thread7
this is thread T this is thread T this is Main Thread8
this is thread T this is thread T this is Main Thread9
False False0__21
False False2
False False3
this is thread T this is thread T this is Main Thread9
False False5
False False6
False False2
False False8
False False9
False False6
False GFG False1
False GFG False2
False GFG False3
False GFG False2
False GFG False5
False GFG False6
False False8
False False0
False GFG False9
False False3
this is thread T this is thread T this is Main Thread9
GFG False True Geeks For Geeks2
False False6
Output:
this is non-daemon thread this is non-daemon thread this is non-daemon thread main Thread execution this is non-daemon thread this is non-daemon thread
Luồng của chủ đề daemon trên luồng không phải daemon
Đây là một ví dụ để hiển thị cách các chủ đề daemon hoạt động trên các luồng không phải là Daemon trong quá trình thực hiện chương trình. Chúng ta đã thấy trong ví dụ trên rằng cách chủ đề không phải Daemon hoàn thành việc thực thi sau khi chấm dứt luồng chính nhưng đây là một cái gì đó khác với điều đó. Trong ví dụ này, chúng tôi đã tạo một hàm ren_1 [] và luồng t giống như ví dụ trên nhưng ở đây sau khi tạo ra luồng t, chúng tôi sử dụng phương thức setDaemon [] Bắt đầu luồng T và tạm thời dừng việc thực hiện luồng chính. Đây là xoắn khi luồng chính hoàn thành việc thực thi và chấm dứt sau đó luồng t cũng chấm dứt vì đây là luồng daemon, trong đó luồng daemon chấm dứt thực thi khi luồng chính kết thúc, công việc của nó là hỗ trợ chủ đề chính nếu có Không còn chủ đề chính tại sao chủ đề daemon sẽ chạy ở đó họ cũng chấm dứt vẫn còn thực thi các hướng dẫn còn lại. thread_1[] and thread T as same as above example but here after the creation of thread T we use setDaemon[] method to change the non-daemon nature of thread T to daemon nature, then we start the thread T and temporary stops the execution of the main thread. Here is the twist when the main thread is complete their execution and terminates then thread T also terminates because this is a daemon thread, where daemon thread terminates it’s execution when the main thread terminates, work of it is to support the main thread if there is no main thread remaining why will daemon thread running there they also terminate still execution of instructions is remaining.
Dưới đây là việc thực hiện: & NBSP;
Python3
from
threading
import
*
import
this is thread T this is thread T this is Main Thread1
this is thread T this is thread T this is Main Thread2
this is thread T this is thread T this is Main Thread3
this is thread T this is thread T this is Main Thread4
this is thread T this is thread T this is Main Thread5
this is thread T this is thread T this is Main Thread6
this is thread T this is thread T this is Main Thread7
this is thread T this is thread T this is Main Thread8
this is thread T this is thread T this is Main Thread9
False False0__21
False False2
False False3
this is thread T this is thread T this is Main Thread9
from
3False False6
False False2
False False8
from
7False False6
False GFG False1
False GFG False2
False GFG False3
False GFG False2
False GFG False5
threading
4threading
5
False GFG False9
threading
7
False False8
False False0
False False6
False False3
this is thread T this is thread T this is Main Thread9
import
3import
4Output:
this is thread T this is thread T this is Main Thread
Luồng của chủ đề daemon trên luồng không phải daemonNon-Daemon
Đây là một ví dụ để hiển thị cách các chủ đề daemon hoạt động trên các luồng không phải là Daemon trong quá trình thực hiện chương trình. Chúng ta đã thấy trong ví dụ trên rằng cách chủ đề không phải Daemon hoàn thành việc thực thi sau khi chấm dứt luồng chính nhưng đây là một cái gì đó khác với điều đó. Trong ví dụ này, chúng tôi đã tạo một hàm ren_1 [] và luồng t giống như ví dụ trên nhưng ở đây sau khi tạo ra luồng t, chúng tôi sử dụng phương thức setDaemon [] Bắt đầu luồng T và tạm thời dừng việc thực hiện luồng chính. Đây là xoắn khi luồng chính hoàn thành việc thực thi và chấm dứt sau đó luồng t cũng chấm dứt vì đây là luồng daemon, trong đó luồng daemon chấm dứt thực thi khi luồng chính kết thúc, công việc của nó là hỗ trợ chủ đề chính nếu có Không còn chủ đề chính tại sao chủ đề daemon sẽ chạy ở đó họ cũng chấm dứt vẫn còn thực thi các hướng dẫn còn lại.
- Dưới đây là việc thực hiện: & NBSP;
this is thread T this is thread T this is Main Thread
2RuntimeError: cannot set daemon status of active thread
1
False
GFG
False
1False
GFG
False
2 threading
1False
GFG
False
2 False
GFG
False
5Program to explain
isDaemon[] and daemon methods.
Phương pháp kiểm tra xem chủ đề có phải là daemon hay không phải daisDaemon[] and daemon method. Here we use current_thread[] method which simplifies which thread is currently executing, and we use it with isDaemon[] and daemon method to check the nature or status of the current thread. The output of this code is False and False because current_thread is the main thread is always a non-daemon thread.
Python3
from
threading
import
*
False False3
*
0False False3
*
2Output:
False False
import
this is thread T
this is thread T
this is Main Thread
1
this is thread T this is thread T this is Main Thread2
this is thread T this is thread T this is Main Thread3thread_1 which is currently a non-daemon thread and non-active thread then we check the status or nature of the thread, the output becomes False after that we start a thread now thread becomes an active thread again we check it’s status an again output is False this all means that not the main thread is non-daemon but also other created thread is also non-daemon.
Python3
from
threading
import
*
this is thread T this is thread T this is Main Thread2
*
8this is thread T this is thread T this is Main Thread4
False False3
this is thread T this is thread T this is Main Thread9
this is thread T this is thread T this is Main Thread0226
this is thread T this is thread T this is Main Thread04
False GFG False2
False GFG False3
False GFG False2
this is thread T this is thread T this is Main Thread08
False False3
this is thread T this is thread T this is Main Thread10
this is thread T this is thread T this is Main Thread11
False False3
this is thread T this is thread T this is Main Thread13
Output:
False GFG False
Thay đổi không daemon thành daemon
Như trước đây chúng ta thấy rằng làm thế nào để kiểm tra xem chủ đề sau đây có phải là daemon hay không daemon hay không, ở đây chúng ta tìm hiểu về cách chủ đề không phải Daemon sau đây có thể được thay đổi thành daemon.
& nbsp; một setdaemon [] là phương pháp được sử dụng để thay đổi bản chất không phải là daemon của một chủ đề nhất định thành bản chất daemon. Phương thức setDaemon [] chỉ lấy một tham số là giá trị boolean [true hoặc false]. & nbsp; setDaemon[] is the method that is used to change the non-daemon nature of a given thread into the daemon nature. setDaemon[] method takes only one parameter that is a Boolean value [True or False].
Cú pháp: Thread_Name.SetDaemon [] & NBSP; & nbsp; Thread_name.setDaemon[]
# Ở đây Thread_Name & nbsp; đề cập đến tên của chủ đề mà bạn đã sử dụng. & nbsp; & nbsp; & nbsp;
Tham số: [Đúng hoặc Sai] & NBSP;[True or False]
- Nếu đúng, đánh dấu chủ đề này là một luồng daemon
- Nếu sai, đánh dấu chủ đề này là một chủ đề không daemon.
Example:
Python3
from
threading
import
*
this is thread T this is thread T this is Main Thread2
this is thread T this is thread T this is Main Thread19
False False2
False False3
this is thread T this is thread T this is Main Thread9
this is thread T this is thread T this is Main Thread23
False False6
False GFG False1
False GFG False2
threading
1False GFG False2
this is thread T this is thread T this is Main Thread29
False False3
this is thread T this is thread T this is Main Thread9
this is thread T this is thread T this is Main Thread02
False False6
False False3
this is thread T this is thread T this is Main Thread35
threading
4threading
5import
4
False False3
this is thread T this is thread T this is Main Thread35
this is thread T this is thread T this is Main Thread41
Output:
GFG False True Geeks For Geeks
Explanation:
Trong ví dụ trên trước tiên, chúng tôi nhập luồng thư viện sau đó chúng tôi xác định một hàm mới Fun [] Tại điểm tiếp theo, chúng tôi tạo một biến luồng mới T. & nbsp; . Tại thời điểm này, đầu ra sẽ đúng. & NBSP;threading then we define a new function fun[] at the next point we create a new threading variable T. Currently, T is a non-active thread we didn’t execute the start[] method yet, here we will check the status of thread T and the output comes to be False at next we use the method setDaemon[] to change the nature of thread T after using the method again we will check the status of following thread at this time output will be True.
Thay đổi luồng chính thành chủ đề daemon
Nếu chúng ta muốn thay đổi chủ đề chính luôn không phải là daemon tự nhiên thành Daemon Nature thì chúng ta sẽ có một thời gian chạy vì khi chương trình được bắt đầu tại một thời điểm chủ đề chính cũng được bắt đầu để luồng chính là một luồng hoạt động và hoạt động Chủ đề không được đặt thành Daemon.RuntimeError because when the program is started at a time main thread is also started so the main thread is an active thread and the active thread is not set to the daemon.
Example:
Python3
from
threading
import
*
False False3
this is thread T this is thread T this is Main Thread47
threading
5this is thread T this is thread T this is Main Thread49
Output:
RuntimeError: cannot set daemon status of active thread