Gọi
Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
0 để nói với chương trình rằng nó nên đợi quá trình này hoàn tất trước khi tiếp tục với phần còn lại của mãfrom multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
Chia sẻ dữ liệu giữa các tiến trình¶
Vì các quy trình không nằm trong cùng một không gian bộ nhớ nên chúng không có quyền truy cập vào cùng một dữ liệu [công khai]. Vì vậy, chúng cần các đối tượng bộ nhớ dùng chung đặc biệt để chia sẻ dữ liệu
Dữ liệu có thể được lưu trữ trong biến bộ nhớ dùng chung bằng cách sử dụng
Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
1 hoặc Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
2
3. Tạo một đối tượngValue at beginning: 0 Array at beginning: [0.0, 100.0, 200.0] Value at end: 144 Array at end: [134.0, 237.0, 339.0] end main
4 kiểuValue at beginning: 0 Array at beginning: [0.0, 100.0, 200.0] Value at end: 144 Array at end: [134.0, 237.0, 339.0] end main
5. Truy cập giá trị bằngValue at beginning: 0 Array at beginning: [0.0, 100.0, 200.0] Value at end: 144 Array at end: [134.0, 237.0, 339.0] end main
6Value at beginning: 0 Array at beginning: [0.0, 100.0, 200.0] Value at end: 144 Array at end: [134.0, 237.0, 339.0] end main
7. Tạo một mảngValue at beginning: 0 Array at beginning: [0.0, 100.0, 200.0] Value at end: 144 Array at end: [134.0, 237.0, 339.0] end main
4 với các phần tử kiểuValue at beginning: 0 Array at beginning: [0.0, 100.0, 200.0] Value at end: 144 Array at end: [134.0, 237.0, 339.0] end main
5. Truy cập các giá trị vớiValue at beginning: 0 Array at beginning: [0.0, 100.0, 200.0] Value at end: 144 Array at end: [134.0, 237.0, 339.0] end main
10from multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
Nhiệm vụ. Tạo hai quy trình, mỗi quy trình phải có quyền truy cập vào một biến được chia sẻ và sửa đổi nó [trong trường hợp này chỉ tăng nó lặp lại 1 trong 100 lần]. Tạo hai quy trình khác chia sẻ một mảng và sửa đổi [tăng] tất cả các phần tử trong mảng
from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
2Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
Cách sử dụng from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
23¶
from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
Lưu ý rằng trong ví dụ trên, 2 quy trình sẽ tăng giá trị được chia sẻ lên 1 trong 100 lần. Điều này dẫn đến tổng số 200 hoạt động. Nhưng tại sao giá trị cuối cùng không phải là 200?
Điều kiện của cuộc đua¶
Một điều kiện cuộc đua đã xảy ra ở đây. Tình trạng dồn đuổi xảy ra khi hai hoặc nhiều tiến trình hoặc luồng có thể truy cập dữ liệu được chia sẻ và chúng cố gắng thay đổi dữ liệu đó cùng một lúc. Trong ví dụ của chúng tôi, hai quy trình phải đọc giá trị được chia sẻ, tăng giá trị đó lên 1 và ghi lại giá trị đó vào biến được chia sẻ. Nếu điều này xảy ra đồng thời, hai quá trình sẽ đọc cùng một giá trị, tăng giá trị đó lên và ghi lại giá trị đó. Do đó, cả hai quy trình đều ghi cùng một giá trị tăng lên vào đối tượng được chia sẻ và giá trị không tăng thêm 2. Xem https. //www. kỹ sư trăn. com/learn/advancedpython16_threading/ để được giải thích chi tiết về điều kiện chủng tộc
Tránh các điều kiện cuộc đua với from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
23¶
from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
Khóa [còn được gọi là mutex] là một cơ chế đồng bộ hóa để thực thi các giới hạn đối với quyền truy cập vào tài nguyên trong môi trường có nhiều quy trình/luồng thực thi. Khóa có hai trạng thái. bị khóa và mở khóa. Nếu trạng thái bị khóa, nó không cho phép các quy trình/luồng đồng thời khác vào phần mã này cho đến khi trạng thái được mở khóa trở lại
Hai chức năng quan trọng. -
from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
13. Điều này sẽ khóa trạng thái và chặn - from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
14. Điều này sẽ mở khóa trạng thái một lần nữaQuan trọng. Bạn phải luôn giải phóng lại khối sau khi có được nó
Trong ví dụ của chúng tôi, phần mã quan trọng nơi biến được chia sẻ được đọc và tăng hiện đã bị khóa. Điều này ngăn quá trình thứ hai sửa đổi đối tượng được chia sẻ cùng một lúc. Không có nhiều thay đổi trong mã của chúng tôi. Tất cả các thay đổi mới được nhận xét trong mã bên dưới
from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
1from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
9Sử dụng khóa làm trình quản lý ngữ cảnh¶
Sau
from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
13, bạn đừng bao giờ quên gọi cho from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
14 để mở khóa mã. Bạn cũng có thể sử dụng khóa làm trình quản lý bối cảnh, khóa này sẽ khóa và mở khóa mã của bạn một cách an toàn. Nên sử dụng khóa theo cách nàyValue at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
2Sử dụng hàng đợi trong Python¶
Dữ liệu cũng có thể được chia sẻ giữa các quy trình với Hàng đợi. Hàng đợi có thể được sử dụng để trao đổi dữ liệu an toàn luồng/an toàn quy trình và xử lý dữ liệu cả trong môi trường đa luồng và đa xử lý, điều đó có nghĩa là bạn có thể tránh phải sử dụng bất kỳ nguyên tắc đồng bộ hóa nào như khóa
Hàng đợi¶
Hàng đợi là một cấu trúc dữ liệu tuyến tính tuân theo nguyên tắc Nhập trước xuất trước [FIFO]. Một ví dụ điển hình là một hàng khách đang xếp hàng chờ đợi, trong đó khách hàng đến trước được phục vụ trước.
Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
3Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
4Sử dụng hàng đợi trong đa xử lý¶
Các hoạt động với một hàng đợi là quá trình an toàn. Hàng đợi đa xử lý thực hiện tất cả các phương thức của hàng đợi. Hàng đợi ngoại trừ
from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
17 và from multiprocessing import Process
import os
def square_numbers[]:
for i in range[1000]:
result = i * i
if __name__ == "__main__":
processes = []
num_processes = os.cpu_count[]
# number of CPUs on the machine. Usually a good choise for the number of processes
# create processes and asign a function for each process
for i in range[num_processes]:
process = Process[target=square_numbers]
processes.append[process]
# start all processes
for process in processes:
process.start[]
# wait for all processes to finish
# block the main programm until these processes are finished
for process in processes:
process.join[]
18. Các phương pháp quan trọng là
19. Xóa và trả lại mục đầu tiên. Theo mặc định, nó chặn cho đến khi có mặt hàngfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
90. Đặt phần tử ở cuối hàng đợi. Theo mặc định, nó sẽ chặn cho đến khi có một vị trí miễn phífrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
91. Trả về True nếu hàng đợi rỗngfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
92. Cho biết rằng quy trình hiện tại sẽ không đưa thêm dữ liệu vào hàng đợi nàyfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
1Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
2Nhóm quy trình¶
Một đối tượng nhóm quy trình kiểm soát một nhóm các quy trình công nhân mà các công việc có thể được gửi tới. Nó hỗ trợ các kết quả không đồng bộ với thời gian chờ và gọi lại và có triển khai bản đồ song song. Nó có thể tự động quản lý các bộ xử lý có sẵn và chia dữ liệu thành các phần nhỏ hơn mà sau đó có thể được xử lý song song bởi các quy trình khác nhau. Xem https. // tài liệu. con trăn. tổ chức/3. 7/thư viện/đa xử lý. html#đa xử lý. pool cho tất cả các phương pháp có thể. Các phương pháp quan trọng là
93. Phương pháp này cắt iterable thành một số khối mà nó gửi đến nhóm quy trình dưới dạng các tác vụ riêng biệt. Kích thước [gần đúng] của các khối này có thể được chỉ định bằng cách đặt kích thước khối thành một số nguyên dương. Nó chặn cho đến khi kết quả sẵn sàngfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
94. Ngăn chặn bất kỳ nhiệm vụ nào khác được gửi đến nhóm. Khi tất cả các tác vụ đã được hoàn thành, các quy trình công nhân sẽ thoátfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
18. Chờ các worker process thoát ra. Người ta phải gọifrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
94 hoặcfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
97 trước khi sử dụngfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
18from multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
99. Gọi func với các đối số args. Nó chặn cho đến khi kết quả sẵn sàng. func chỉ được thực hiện trong MỘT trong số các công nhân của nhómfrom multiprocessing import Process import os def square_numbers[]: for i in range[1000]: result = i * i if __name__ == "__main__": processes = [] num_processes = os.cpu_count[] # number of CPUs on the machine. Usually a good choise for the number of processes # create processes and asign a function for each process for i in range[num_processes]: process = Process[target=square_numbers] processes.append[process] # start all processes for process in processes: process.start[] # wait for all processes to finish # block the main programm until these processes are finished for process in processes: process.join[]
Ghi chú. Ngoài ra còn có các biến thể không đồng bộ
Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
20 và Value at beginning: 0
Array at beginning: [0.0, 100.0, 200.0]
Value at end: 144
Array at end: [134.0, 237.0, 339.0]
end main
21 sẽ không chặn. Họ có thể thực hiện các cuộc gọi lại khi kết quả đã sẵn sàng