Hướng dẫn what is multi thread in python - đa luồng trong python là gì
Bài viết này bao gồm những điều cơ bản của đa luồng trong ngôn ngữ lập trình Python. Giống như đa xử lý, đa luồng là một cách để đạt được đa nhiệm. Trong đa luồng, khái niệm về chủ đề được sử dụng. Trước tiên chúng ta hãy hiểu khái niệm về chủ đề trong kiến trúc máy tính.threads is used. Let us first understand the concept of thread in computer architecture. Show Chủ đề Trong điện toán, một quy trình là một ví dụ của một chương trình máy tính đang được thực thi. Bất kỳ quá trình nào cũng có 3 thành phần cơ bản:process is an instance of a computer program that is being executed. Any process has 3 basic components:
Một luồng là một thực thể trong một quy trình có thể được lên lịch để thực hiện. Ngoài ra, đây là đơn vị xử lý nhỏ nhất có thể được thực hiện trong hệ điều hành (hệ điều hành). Nói một cách đơn giản, một luồng là một chuỗi các hướng dẫn như vậy trong một chương trình có thể được thực thi độc lập với mã khác. Để đơn giản, bạn có thể cho rằng một chủ đề chỉ đơn giản là một tập hợp con của một quá trình! Một luồng chứa tất cả thông tin này trong khối điều khiển luồng (TCB):thread is an entity within a process that can be scheduled for execution. Also, it is the smallest unit of processing that can be performed in an OS (Operating System). In simple words, a thread is a sequence of such instructions within a program that can be executed independently of other code. For simplicity, you can assume that a thread is simply a subset of a process! A thread contains all this information in a Thread Control Block (TCB):
Hãy xem xét sơ đồ dưới đây để hiểu mối quan hệ giữa quy trình và luồng của nó: Đa luồng: Nhiều luồng có thể tồn tại trong một quá trình trong đó:Multiple threads can exist within one process where:
Hãy xem xét sơ đồ dưới đây để hiểu làm thế nào nhiều luồng tồn tại trong bộ nhớ: MultiThreading được định nghĩa là khả năng của bộ xử lý để thực hiện đồng thời nhiều luồng. is defined as the ability of a processor to execute multiple threads concurrently.
Hãy xem xét sơ đồ dưới đây trong đó một quy trình chứa hai luồng hoạt động: & nbsp; Đa luồng trong PythonTrong Python, mô -đun luồng cung cấp API rất đơn giản và trực quan để sinh ra nhiều luồng trong một chương trình. Chúng ta hãy xem xét một ví dụ đơn giản bằng cách sử dụng mô -đun luồng: & nbsp;threading module provides a very simple and intuitive API for spawning multiple threads in a program. Let us consider a simple example using a threading module: Python3
import threading0 import threading1 import threading2 import threading3 import threading4 import threading5 import threading0 t1.start() t2.start()4 import threading2 import threading3 import threading4 t1.start() t2.start()8 import threading6 import threading7 import threading8 import threading9 t1.start() t2.start()2 t1.join() t2.join()4 t1.join() t2.join()5 t1.join() t2.join()6 t1.join() t2.join()6 t1.join() t2.join()8 t1.join() t2.join()9 import threading2 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117581 t1.join() t2.join()6 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117583 t1.join() t2.join()6 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117585 t1.join() t2.join()6 import threading4 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117588 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117589 import threading2 print("ID of process running main program: {}".format(os.getpid()))1 t1.join() t2.join()6 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117583 t1.join() t2.join()6 print("ID of process running main program: {}".format(os.getpid()))5 t1.join() t2.join()6 import threading4 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117588 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117589 import threading2 print("Main thread name: {}".format(threading.main_thread().name))1 import threading2 print("Main thread name: {}".format(threading.main_thread().name))3 import threading2 print("Main thread name: {}".format(threading.main_thread().name))5 import threading2 print("Main thread name: {}".format(threading.main_thread().name))7 import threading2 import threading3 import threading4 print("Task 1 assigned to thread: {}".format(threading.current_thread().name))1 print("Task 1 assigned to thread: {}".format(threading.current_thread().name))2 Square: 100 Cube: 1000 Done! Hãy để chúng tôi cố gắng hiểu mã trên:
import threading
t1.start() t2.start()
t1.join() t2.join()
Hãy xem xét sơ đồ dưới đây để hiểu rõ hơn về cách thức hoạt động của chương trình: & NBSP; Hãy xem xét chương trình Python được đưa ra dưới đây, trong đó chúng tôi in tên luồng và quy trình tương ứng cho từng nhiệm vụ: & nbsp; Python3
import threading0 import threading1 import threading2 import threading3 import threading4 import threading5 import threading0 t1.start() t2.start()4 import threading2 import threading3 import threading4 t1.start() t2.start()8 import threading6 import threading7 import threading8 import threading9 t1.start() t2.start()2 import threading0 threading 6import threading2 import threading3 import 1import threading00 import 3import threading7 import 5import threading2 import threading3 import 8___t1.join() t2.join()4 t1.join() t2.join()5 t1.join() t2.join()6 t1.join() t2.join()6 import threading17 import threading2 import threading3 import 8import 9 import threading222__17 import threading2 import threading3 import threading27 import threading7 import 5import threading2 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117581 t1.join() t2.join()6 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117583 t1.join() t2.join()6 import threading35 t1.join() t2.join()6 import threading37 print("Task 1 assigned to thread: {}".format(threading.current_thread().name))2 import threading2 print("ID of process running main program: {}".format(os.getpid()))1 t1.join() t2.join()6 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 117583 t1.join() t2.join()6__ import threading2 print("Main thread name: {}".format(threading.main_thread().name))1 import threading2 print("Main thread name: {}".format(threading.main_thread().name))3 import threading2 print("Main thread name: {}".format(threading.main_thread().name))5 import threading2 print("Main thread name: {}".format(threading.main_thread().name))7 ID of process running main program: 11758 Main thread name: MainThread Task 1 assigned to thread: t1 ID of process running task 1: 11758 Task 2 assigned to thread: t2 ID of process running task 2: 11758 Hãy để chúng tôi cố gắng hiểu mã trên:
print("ID of process running main program: {}".format(os.getpid()))
print("Main thread name: {}".format(threading.main_thread().name))
print("Task 1 assigned to thread: {}".format(threading.current_thread().name)) Sơ đồ được đưa ra dưới đây xóa khái niệm trên:
Bài viết này được đóng góp bởi Nikhil Kumar. Nếu bạn thích GeekSforGeeks và muốn đóng góp, bạn cũng có thể viết một bài viết bằng Write.GeekSforGeek.org hoặc gửi bài viết của bạn. Xem bài viết của bạn xuất hiện trên trang chính của GeekSforGeek và giúp các chuyên viên máy tính khác.Nikhil Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác, hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên. Nhiều chủ đề trong Python là gì?MultiThreading đề cập đến việc thực hiện đồng thời nhiều luồng bằng cách nhanh chóng chuyển đổi điều khiển CPU giữa các luồng (được gọi là chuyển đổi ngữ cảnh). Khóa thông dịch viên toàn cầu Python giới hạn một luồng để chạy tại một thời điểm ngay cả khi máy chứa nhiều bộ xử lý.concurrently executing multiple threads by rapidly switching the control of the CPU between threads (called context switching). The Python Global Interpreter Lock limits one thread to run at a time even if the machine contains multiple processors.
Đa chủ đề được sử dụng để làm gì?MultiThreading là khả năng của một chương trình hoặc hệ điều hành để kích hoạt nhiều người dùng cùng một lúc mà không yêu cầu nhiều bản sao của chương trình chạy trên máy tính.MultiThreading cũng có thể xử lý nhiều yêu cầu từ cùng một người dùng.enable more than one user at a time without requiring multiple copies of the program running on the computer. Multithreading can also handle multiple requests from the same user.
Multi threading với ví dụ là gì?MultiThreading là gì?MultiThreading cho phép chúng tôi chạy nhiều luồng đồng thời.Ví dụ: trong trình duyệt web, chúng ta có thể có một luồng xử lý giao diện người dùng và song song chúng ta có thể có một luồng khác tìm hiểu dữ liệu sẽ được hiển thị.Vì vậy, đa luồng cải thiện khả năng đáp ứng của một hệ thống.enables us to run multiple threads concurrently. For example in a web browser, we can have one thread which handles the user interface, and in parallel we can have another thread which fetches the data to be displayed. So multithreading improves the responsiveness of a system.
Là đa luồng tốt trong Python?Để tóm tắt lại, luồng trong Python cho phép nhiều luồng được tạo trong một quy trình duy nhất, nhưng do Gil, không ai trong số chúng sẽ chạy cùng một lúc.Chủ đề vẫn là một lựa chọn rất tốt khi chạy nhiều tác vụ ràng buộc I/O đồng thời.Threading is still a very good option when it comes to running multiple I/O bound tasks concurrently. |