Python Learning notes-shared memory in multi-process value & Array

Source: Internet
Author: User

1 Shared memory

Basic Features:

(1) Shared memory is one of the most efficient inter-process communication methods, and processes can read and write directly to memory without requiring any copy of the data.

(2) In order to exchange information between multiple processes, the kernel specifically leaves out a chunk of memory that can be mapped to its own private address space by the process that needs to be accessed. The process can read and write directly to this piece of memory without having to copy the data, thereby greatly improving efficiency. ( file Map )

(3) Because multiple processes share a piece of memory, they also need to rely on some kind of synchronization mechanism.

Advantages and Disadvantages

Pros: Quickly pass data between processes

Cons: Risk in data security, content in memory is overwritten or tampered with by other processes

Note: Often used in conjunction with synchronous mutexes

2 Basic Syntax

Shared memory to conform to C language usage syntax

From multiprocessing import Value, Array

Value : stores a value in memory,

Array: Stores multiple data in memory, but requires consistent data types

Supplemental: Data type

Type Code C Type Python Type Minimum size in bytes Notes
‘b‘ Signed Char Int 1
‘B‘ unsigned char Int 1
‘u‘ Py_unicode Unicode character 2 (1)
‘h‘ Signed Short Int 2
‘H‘ unsigned short Int 2
‘i‘ Signed int Int 2
‘I‘ unsigned int Int 2
‘l‘ Signed Long Int 4
‘L‘ unsigned long Int 4
‘q‘ Signed Long Long Int 8 (2)
‘Q‘ unsigned long long Int 8 (2)
‘f‘ Float Float 4
‘d‘ Double Float 8

Specific reference:8.7. array-efficient Arrays of numeric values

2.1 Value

Value (Typecode_or_type, *args, lock=true)

Function: Get a shared memory object and deposit the initial value, method of multiprocessing

Returns returns a synchronized shared object (synchronous share objects)

Typecode_or_type: Defines the return type (converted to a storage type in the C language), either a ctypes type or a code representing the cTYPES type.

*args: Opens up a space and assigns an args value, which is worth an unlimited number of types

Note: cTYPES is an external library of Python that provides data types that are compatible with the C language, can call functions of DLLs or shared libraries, and can be used to wrap these libraries in Python.

 fromMultiprocessingImportProcess,valueImport TimeImportRandomdefSave_money (Money): forIinchRange (100): Time.sleep (0.1) Money.value+ = Random.randint (1,200)defTake_money (Money): forIinchRange (100): Time.sleep (0.1) Money.value-= Random.randint (1,150)#Money is a shared memory object, give him an initial value of 2000, the type is positive type "I"#equivalent to opening up a space, while binding the value ofMoney = Value ('I', 2000) d= Process (target=save_money,args= (Money,))#Money In this is global, not written can alsoD.start () W= Process (target=take_money,args= (Money,))#Money In this is global, not written can alsoW.start () d.join () W.join ( )Print(Money.value)

Run 4491

2.2 Array

Array (Typecode_or_type, Size_or_initializer, *, lock=true)

Use basically similar to value,returns a synchronized shared array

Typecode_or_type: Defines the type of storage that is converted to the C language;

Size_or_initializer: Initializes the shared memory space,

A number that represents the amount of space in the open shared memory, (value indicates that a numeric value is bound for the space)

Array, which indicates that it is stored in shared memory

 from Import Process,array def Fun (m,n):      for inch range (N):         Print (M[i]) # here No table number 8 type is integral type ' I '; # indicates that 8 spaces are opened, and all are integer I, which is actually a list m = Array ('i',3 = Process (target= fun,args= (m,4)) P.start () P.join ()

Run

0 0 0Process Process-1: .... Indexerror:invalid Index

Description: Three 0 means that the open shared memory capacity is 3, and when it exceeds 3 o'clock, an error will be added.

Example 2

 fromMultiprocessingImportProcess,arrayImport TimedefFun (m,n): forIinchrange (N): M[i]=im= Array ('I', 5) P= Process (target= fun,args= (m,5) ) P.start () Time.sleep (1) forIinchm:Print(i) p.join ()

Run results

1 2 3 4

If time.sleep (1) is removed, the output is 0, because it has already been printed without assigning a value.

 fromMultiprocessingImportProcess,arrayImport TimedefFun (m,n): forIinchrange (n):Print(M[i]) m[i]=I#This means opening up to 5 spaces and storing the elements in the listm = Array ('I', [1,2,3,4,5]) P= Process (target= fun,args= (m,5) ) P.start () Time.sleep (1) forIinchm:Print(i) p.join ()

Run

1 2 3 4 51 2 3 4

The second parameter, if passed in a number, indicates how much space is being opened in shared memory.

If a list is passed in, it opens up the amount of space in the response element and stores it directly into the shared space

Python Learning notes-shared memory in multi-process value & Array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.