Viết tập tin đa xử lý python

Xin chào


Trên cửa sổ, với PyWin32, để đọc mã mẫu nhỏ này


thời gian nhập
nhập tệp win32, win32con, pywintypes

def bầy [tệp].
hfile = win32file. _get_osfhandle[tệp. fileno[]]
tệp win32. LockFileEx[hfile, win32con. LOCKFILE_EXCLUSIVE_LOCK, 0, 0xffff,
pywintypes. OVERLAPPED[]]

def funlock[file].
hfile = win32file. _get_osfhandle[tệp. fileno[]]
tệp win32. UnlockFileEx[hfile, 0, 0xffff, pywintypes. OVERLAPPED[]]


file = open["FLock. txt", "r+"]
tệp [tệp]
tệp. seek[123]
for i in range[500].
tệp. write["AAAAAAAAAA"]
in
thời gian. ngủ [0. 001]

#funlock[tệp]
tệp. đóng[]


Michel Claveau

Mô-đun này cung cấp một lớp,

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1, để phân bổ và quản lý bộ nhớ dùng chung được truy cập bởi một hoặc nhiều quy trình trên máy đa xử lý hoặc đa xử lý đối xứng [SMP]. Để hỗ trợ quản lý vòng đời của bộ nhớ dùng chung, đặc biệt là trên các quy trình riêng biệt, một lớp con
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
2,
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
3, cũng được cung cấp trong mô-đun
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
4

Trong mô-đun này, bộ nhớ dùng chung đề cập đến các khối bộ nhớ dùng chung "kiểu hệ thống V" [mặc dù không nhất thiết phải được triển khai rõ ràng như vậy] và không đề cập đến "bộ nhớ dùng chung phân tán". Kiểu bộ nhớ dùng chung này cho phép các quy trình riêng biệt có khả năng đọc và ghi vào một vùng chung [hoặc dùng chung] của bộ nhớ dễ bay hơi. Theo quy ước, các quy trình bị giới hạn chỉ có quyền truy cập vào không gian bộ nhớ quy trình của riêng chúng nhưng bộ nhớ dùng chung cho phép chia sẻ dữ liệu giữa các quy trình, thay vào đó tránh phải gửi tin nhắn giữa các quy trình chứa dữ liệu đó. Chia sẻ dữ liệu trực tiếp qua bộ nhớ có thể mang lại lợi ích hiệu suất đáng kể so với chia sẻ dữ liệu qua đĩa hoặc ổ cắm hoặc các giao tiếp khác yêu cầu tuần tự hóa/giải tuần tự hóa và sao chép dữ liệu

lớp đa xử lý. shared_memory. Bộ nhớ dùng chung[tên=Không có , tạo=Sai , size=0]

Tạo khối bộ nhớ dùng chung mới hoặc gắn vào khối bộ nhớ dùng chung hiện có. Mỗi khối bộ nhớ dùng chung được gán một tên duy nhất. Theo cách này, một tiến trình có thể tạo một khối bộ nhớ dùng chung với một tên cụ thể và một tiến trình khác có thể đính kèm vào cùng một khối bộ nhớ dùng chung đó bằng chính tên đó

Là tài nguyên để chia sẻ dữ liệu giữa các quy trình, các khối bộ nhớ dùng chung có thể tồn tại lâu hơn quy trình ban đầu đã tạo ra chúng. Khi một tiến trình không còn cần quyền truy cập vào khối bộ nhớ dùng chung mà các tiến trình khác vẫn có thể cần, phương thức

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
5 sẽ được gọi. Khi một khối bộ nhớ dùng chung không còn cần thiết cho bất kỳ quy trình nào, phương thức
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
6 sẽ được gọi để đảm bảo dọn dẹp thích hợp

tên là tên duy nhất cho bộ nhớ dùng chung được yêu cầu, được chỉ định dưới dạng một chuỗi. Khi tạo một khối bộ nhớ dùng chung mới, nếu tên

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
7 [mặc định] được cung cấp, thì một tên mới sẽ được tạo

tạo điều khiển xem khối bộ nhớ dùng chung mới được tạo [

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
8] hay khối bộ nhớ dùng chung hiện có được đính kèm [
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
9]

kích thước chỉ định số byte được yêu cầu khi tạo khối bộ nhớ dùng chung mới. Do một số nền tảng chọn phân bổ khối bộ nhớ dựa trên kích thước trang bộ nhớ của nền tảng đó nên kích thước chính xác của khối bộ nhớ dùng chung có thể lớn hơn hoặc bằng kích thước được yêu cầu. Khi gắn vào khối bộ nhớ dùng chung hiện có, tham số

>>> from multiprocessing.managers import SharedMemoryManager
>>> smm = SharedMemoryManager[]
>>> smm.start[]  # Start the process that manages the shared memory blocks
>>> sl = smm.ShareableList[range[4]]
>>> sl
ShareableList[[0, 1, 2, 3], name='psm_6572_7512']
>>> raw_shm = smm.SharedMemory[size=128]
>>> another_sl = smm.ShareableList['alpha']
>>> another_sl
ShareableList[['a', 'l', 'p', 'h', 'a'], name='psm_6572_12221']
>>> smm.shutdown[]  # Calls unlink[] on sl, raw_shm, and another_sl
0 bị bỏ qua

đóng[]

Đóng quyền truy cập vào bộ nhớ dùng chung từ trường hợp này. Để đảm bảo dọn dẹp tài nguyên hợp lý, tất cả các phiên bản nên gọi

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
5 sau khi phiên bản không còn cần thiết. Lưu ý rằng việc gọi
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
5 không làm cho khối bộ nhớ dùng chung bị hủy

hủy liên kết[]

Yêu cầu phá hủy khối bộ nhớ dùng chung bên dưới. Để đảm bảo dọn dẹp tài nguyên hợp lý, nên gọi ____________ một lần [và chỉ một lần] trên tất cả các quy trình cần khối bộ nhớ dùng chung. Sau khi yêu cầu hủy, khối bộ nhớ dùng chung có thể bị hủy ngay lập tức hoặc không và hành vi này có thể khác nhau giữa các nền tảng. Các nỗ lực truy cập dữ liệu bên trong khối bộ nhớ dùng chung sau khi gọi

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
6 có thể dẫn đến lỗi truy cập bộ nhớ. Ghi chú. quá trình cuối cùng từ bỏ việc giữ nó trên một khối bộ nhớ dùng chung có thể gọi
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
6 và
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
5 theo một trong hai thứ tự

buf

Một bộ nhớ xem nội dung của khối bộ nhớ dùng chung

tên

Quyền truy cập chỉ đọc vào tên duy nhất của khối bộ nhớ dùng chung

kích thước

Quyền truy cập chỉ đọc đối với kích thước tính bằng byte của khối bộ nhớ dùng chung

Ví dụ sau minh họa việc sử dụng phiên bản

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1 ở mức độ thấp

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
7

Ví dụ sau minh họa cách sử dụng thực tế của lớp

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1 với mảng NumPy, truy cập cùng một
>>> from multiprocessing.managers import SharedMemoryManager
>>> smm = SharedMemoryManager[]
>>> smm.start[]  # Start the process that manages the shared memory blocks
>>> sl = smm.ShareableList[range[4]]
>>> sl
ShareableList[[0, 1, 2, 3], name='psm_6572_7512']
>>> raw_shm = smm.SharedMemory[size=128]
>>> another_sl = smm.ShareableList['alpha']
>>> another_sl
ShareableList[['a', 'l', 'p', 'h', 'a'], name='psm_6572_12221']
>>> smm.shutdown[]  # Calls unlink[] on sl, raw_shm, and another_sl
9 từ hai trình bao Python riêng biệt

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end

lớp đa xử lý. quản lý. SharedMemoryManager[[địa chỉ[ , authkey]]]

Một lớp con của

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
2 có thể được sử dụng để quản lý các khối bộ nhớ dùng chung giữa các quy trình

Một cuộc gọi đến

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
21 trên một phiên bản
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
3 khiến một quy trình mới được bắt đầu. Mục đích duy nhất của quy trình mới này là quản lý vòng đời của tất cả các khối bộ nhớ dùng chung được tạo thông qua nó. Để kích hoạt giải phóng tất cả các khối bộ nhớ dùng chung do quy trình đó quản lý, hãy gọi
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
23 trong ví dụ. Điều này kích hoạt một cuộc gọi
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
24 trên tất cả các đối tượng
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1 được quản lý bởi quy trình đó và sau đó tự dừng quy trình đó. Bằng cách tạo các phiên bản
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1 thông qua một
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
3, chúng tôi tránh phải theo dõi và kích hoạt giải phóng tài nguyên bộ nhớ dùng chung theo cách thủ công

Lớp này cung cấp các phương thức để tạo và trả về các thể hiện

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1 và để tạo một đối tượng dạng danh sách [
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29] được hỗ trợ bởi bộ nhớ dùng chung

Tham khảo

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
10 để biết mô tả về địa chỉ kế thừa và đối số đầu vào tùy chọn authkey và cách chúng có thể được sử dụng để kết nối với dịch vụ
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
3 hiện có từ các quy trình khác

Bộ nhớ dùng chung[kích thước]

Tạo và trả về một đối tượng

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1 mới với
>>> from multiprocessing.managers import SharedMemoryManager
>>> smm = SharedMemoryManager[]
>>> smm.start[]  # Start the process that manages the shared memory blocks
>>> sl = smm.ShareableList[range[4]]
>>> sl
ShareableList[[0, 1, 2, 3], name='psm_6572_7512']
>>> raw_shm = smm.SharedMemory[size=128]
>>> another_sl = smm.ShareableList['alpha']
>>> another_sl
ShareableList[['a', 'l', 'p', 'h', 'a'], name='psm_6572_12221']
>>> smm.shutdown[]  # Calls unlink[] on sl, raw_shm, and another_sl
0 được chỉ định theo byte

Danh sách có thể chia sẻ[trình tự]

Tạo và trả về một đối tượng

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29 mới, được khởi tạo bởi các giá trị từ đầu vào
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
15

Ví dụ sau minh họa các cơ chế cơ bản của một

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
3

>>> from multiprocessing.managers import SharedMemoryManager
>>> smm = SharedMemoryManager[]
>>> smm.start[]  # Start the process that manages the shared memory blocks
>>> sl = smm.ShareableList[range[4]]
>>> sl
ShareableList[[0, 1, 2, 3], name='psm_6572_7512']
>>> raw_shm = smm.SharedMemory[size=128]
>>> another_sl = smm.ShareableList['alpha']
>>> another_sl
ShareableList[['a', 'l', 'p', 'h', 'a'], name='psm_6572_12221']
>>> smm.shutdown[]  # Calls unlink[] on sl, raw_shm, and another_sl

Ví dụ sau đây mô tả một mẫu có khả năng thuận tiện hơn để sử dụng các đối tượng

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
3 thông qua câu lệnh
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
18 để đảm bảo rằng tất cả các khối bộ nhớ dùng chung được giải phóng sau khi chúng không còn cần thiết

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
2

Khi sử dụng

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
3 trong câu lệnh
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
18, tất cả các khối bộ nhớ dùng chung được tạo bằng trình quản lý đó đều được giải phóng khi khối mã của câu lệnh
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
18 kết thúc thực thi

lớp đa xử lý. shared_memory. Danh sách có thể chia sẻ[trình tự=Không có , * , tên=Không có]

Cung cấp một đối tượng giống như danh sách có thể thay đổi trong đó tất cả các giá trị được lưu trữ bên trong được lưu trữ trong khối bộ nhớ dùng chung. Điều này ràng buộc các giá trị có thể lưu trữ chỉ với các loại dữ liệu tích hợp sẵn

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
52,
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
53,
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
54,
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
55 [dưới 10M byte],
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
56 [dưới 10M byte]. Nó cũng khác biệt đáng kể so với loại
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
58 tích hợp ở chỗ các danh sách này không thể thay đổi độ dài tổng thể của chúng [i. e. không nối thêm, chèn, v.v. ] và không hỗ trợ tạo động các phiên bản
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29 mới thông qua cắt

trình tự được sử dụng để điền một

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29 mới có đầy đủ các giá trị. Đặt thành
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
7 để thay vào đó đính kèm vào một
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29 đã tồn tại bằng tên bộ nhớ dùng chung duy nhất của nó

tên là tên duy nhất cho bộ nhớ dùng chung được yêu cầu, như được mô tả trong định nghĩa cho

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1. Khi đính kèm vào một
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29 hiện có, hãy chỉ định tên duy nhất của khối bộ nhớ dùng chung trong khi đặt
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
15 thành
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
7

số lượng[giá trị]

Trả về số lần xuất hiện của

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
07

chỉ mục[giá trị]

Trả về vị trí chỉ mục đầu tiên của

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
07. Tăng
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
09 nếu không có
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
07

định dạng

Thuộc tính chỉ đọc chứa định dạng đóng gói

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
41 được sử dụng bởi tất cả các giá trị hiện được lưu trữ

shm

Phiên bản

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1 nơi các giá trị được lưu trữ

Ví dụ sau minh họa cách sử dụng cơ bản của phiên bản

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1

Ví dụ sau mô tả cách một, hai hoặc nhiều quy trình có thể truy cập cùng một

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29 bằng cách cung cấp tên của khối bộ nhớ dùng chung đằng sau nó

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
5

Các ví dụ sau đây chứng minh rằng các đối tượng

>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
29 [và bên dưới
>>> # In the first Python interactive shell
>>> import numpy as np
>>> a = np.array[[1, 1, 2, 3, 5, 8]]  # Start with an existing NumPy array
>>> from multiprocessing import shared_memory
>>> shm = shared_memory.SharedMemory[create=True, size=a.nbytes]
>>> # Now create a NumPy array backed by shared memory
>>> b = np.ndarray[a.shape, dtype=a.dtype, buffer=shm.buf]
>>> b[:] = a[:]  # Copy the original data into shared memory
>>> b
array[[1, 1, 2, 3, 5, 8]]
>>> type[b]

>>> type[a]

>>> shm.name  # We did not specify a name so one was chosen for us
'psm_21467_46075'

>>> # In either the same shell or a new Python shell on the same machine
>>> import numpy as np
>>> from multiprocessing import shared_memory
>>> # Attach to the existing shared memory block
>>> existing_shm = shared_memory.SharedMemory[name='psm_21467_46075']
>>> # Note that a.shape is [6,] and a.dtype is np.int64 in this example
>>> c = np.ndarray[[6,], dtype=np.int64, buffer=existing_shm.buf]
>>> c
array[[1, 1, 2, 3, 5, 8]]
>>> c[-1] = 888
>>> c
array[[  1,   1,   2,   3,   5, 888]]

>>> # Back in the first Python interactive shell, b reflects this change
>>> b
array[[  1,   1,   2,   3,   5, 888]]

>>> # Clean up from within the second Python shell
>>> del c  # Unnecessary; merely emphasizing the array is no longer used
>>> existing_shm.close[]

>>> # Clean up from within the first Python shell
>>> del b  # Unnecessary; merely emphasizing the array is no longer used
>>> shm.close[]
>>> shm.unlink[]  # Free and release the shared memory block at the very end
1] có thể được chọn và bỏ chọn nếu cần. Lưu ý rằng nó vẫn sẽ là cùng một đối tượng được chia sẻ. Điều này xảy ra bởi vì đối tượng được giải tuần tự hóa có cùng tên duy nhất và chỉ được gắn vào một đối tượng hiện có có cùng tên [nếu đối tượng vẫn còn tồn tại]

Chủ Đề