Hướng dẫn which module is used for multithreading in python? - mô-đun nào được sử dụng cho đa luồng trong python?
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. Mô -đun nào được sử dụng để đa luồng?__Thread Mô-đun: Nó cung cấp một triển khai cấp thấp để xâu chuỗi và bị lỗi thời. Mô-đun luồng: Nó cung cấp một triển khai cấp cao cho đa luồng và là tiêu chuẩn hiện tại.
Làm thế nào để bạn sử dụng đa luồng trong Python?Mô-đun luồng được cung cấp với Python bao gồm một cơ chế khóa đơn giản để thực hiện cho phép bạn đồng bộ hóa các luồng. Một khóa mới được tạo bằng cách gọi phương thức khóa (), trả về khóa mới. Phương pháp thu nhận (chặn) của đối tượng khóa mới được sử dụng để buộc các luồng để chạy đồng bộ.
Mô -đun nào hỗ trợ sử dụng luồng trong Python?Mô-đun luồng mô-đun luồng mới hơn được bao gồm với Python 2.4 cung cấp hỗ trợ cấp cao, mạnh mẽ hơn nhiều cho các luồng so với mô-đun luồng được thảo luận trong phần trước.luồng.ActiveCount () - Trả về số lượng đối tượng luồng đang hoạt động.
The newer threading module included with Python 2.4 provides much more powerful, high-level support for threads than the thread module discussed in the previous section. threading. activeCount() − Returns the number of thread objects that are active.
Chúng ta có thể làm đa luồng trong Python không?Python không hỗ trợ đa luồng vì Python trên trình thông dịch Cpython không hỗ trợ thực thi đa lõi thực sự thông qua đa luồng.Tuy nhiên, Python có một thư viện luồng.Gil không ngăn chặn luồng. because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading. |