Python multi-process shared variable value using tips

Source: Internet
Author: User

Objective:

 When using multiple processes with tornado, multiple processes are required to share a state variable, so consider using multiprocessing. Value(please refer to the relevant information for the specific details of the variable). In the use of value on the Internet, because the share is a string, but the introduction of the Internet is an integer or character, so encountered a lot of obstacles, through the search official documents to obtain a solution.

First, the value of the constructor:

the initialization of value is very simple, just like   The Value(' d ', 0.0) can be constructed by:

multiprocessing. value typecode_or_type ,   *args [,  lock ]).

< Span class= "p" > This method returns a < Span class= "Pre" >ctypes   object, Defines the type of return. It is either a ctypes type or a code that represents the cTYPES type. For example c_bool and ' B ' are the same, because ' B ' is c_bool code.

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

  *args is a construction parameter passed to the cTYPES

Second, the use of value

  For shared integers or single characters, initialization is simpler and reference mappings can be done. such as I = value (' i ', 1), c = value (' C ', ' 0 ').  

Note that if the code we use does not exist on the above table, it will be thrown:

Size = ctypes.sizeof (type_)

Typeerror:this type has no size

If you are sharing a string, you cannot find a mapping relationship in the table above, or no code is available. So we need to use the original CType type

For example

From ctypes import c_char_p

SS = Value (c_char_p, ' SS ')

CType types are available from the table below

  

  

Python multi-process shared variable value using tips

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.