Day 12 python learning Notes

1 concurrent programming

    1.1 concept

        1.1.1 Concurrent Programming

            Synchronization perform multiple tasks

            Note: For single-core CPU, and can not realize the true meaning of concurrent programming

    1.2 Process

        Running instance of a program

        Each process has its own address space, memory, data marts and auxiliary data

    1.3 thread

        Within the same process, it can be activated by the parallel control flow

        Share the same context (the address space, a data structure)

        Features: facilitate the communication and sharing of information

              Differences thread execution order may lead to different results

    1.4 python global interpreter (GIL)

        Features: code control by the virtual machine (interpreter main loop)

              Only one main loop control thread execution

               Python interpreter characteristics rather than language features

        Note: For data-intensive, the processing may be employed language such as C, or a multi-process, and for I / O intensive intensive, there is no need

2 Multi-threaded

    2.1 _thread module

        2.1.1 Features

             There is no end of the process control mechanism

             Only a synchronization primitives

        Note: This module begins with an underscore representatives has been deprecated

    2.1.2 Syntax

        

import _thread

_thread.start_new_thread(function,args,**kwargs=none)

 

        Which function as a function of the thread to be executed, args need to write in the format of tuples as a function of parameters, parameter

    2.2 .threading module

        2.2.1 construction thread

             threading.thread (target = function, agrs = parameter)

             Note: There is a way to customize the thread derived class, which override the run method

        The method 2.2.2 Thread

             .start () start the thread

             .join () requires the main thread wait

             .name Thread name

        2.2.3 Get the current thread

             threading.current_thread () Gets the current thread

        2.2.4 synchronization primitives: Lock

             Lock name = Threading.Lock () is defined

             Lock name .require () to obtain

             Lock name .release () release

             Note: Support the context of the operation, with lock name:

3 queue

3.1 module

        queue module, into Queue (FIFO), LifoQueue (LIFO), PriorityQueue (priority queue), etc.

    3.2 Queue Queue

        Configuration example: queue.Queue ()

        Put items: .put (item, block = True, timeout = none)

        Out data items: .get (block = True, timeout = none)

        The current mission statement queue processed: .task_done ()

        All items processed before queue blocking queue: .join ()

4 multiprocessing module

    4.1 Description

    In the python because of the existence of global interpreter lock, the main program can only exist above a process. For I / O processing, the interpreter will immediately release the lock, it is suitable to do the processing python, and for data-intensive, may require the use of low-level language for processing, if it is in python, the multiprocessing module with processing, full use of the capacity of multi-core CPU.

    4.2 references

        import multiprocessing

    4.3 Method

        And threading are very similar, the main methods include:

             multiprocessing.Process (target = function, agrs = parameter) construction process

             Process name .start () to start the process

             Process name .join () waits for the main process requirements

             .name acquisition process name

             multiprocessing.current_process () Gets the current process

5 concurrent.futures模块

    5.1 explanation

        After this module is equivalent to multithreading and multi-process operations were centralized, import this module, multi-threaded and multi-process can go use.

    5.2 concurrent.futures use multiple threads or processes

        

import concurrent.futures

with concurrent.futures.ThreadPoolExecutor(max_workers=数量) as executor:

    executor.submit(函数名,参数)

 

        Note: The multi-process and multi-threaded defined in different, there is no need to fill in target and args

6 decorator

    6.1 Features

        Clearer syntax

        Better code consistency

        Better code maintenance

        NOTE: decorative function definition into a class definition and decorators

    6.2 For the given variables, the need to perform different functions according to the situation, at this time may be implemented using nested if, table or dictionary, recommended entrustment

    6.3 function definition decorator

        Primitive:

        

def greeting(name):

    print(name)

 

        Now enter the desired function to make some changes, plus hello

        

def pre_add(fun):

    def ccc(*args,**kwargs):

        return ‘hello ’+fun(*args,**kwargs)

    return ccc    

 

        In front of the original function of the increase @pre_add field, call the greeting function will output hello name of

    6.4 class definition decorator

        Primitive:

        

def greeting(name):

    print(name)

 

        Now enter the desired function to make some changes, plus hello

        

class pp():

    def __init__(self,func):

        self.func=func

    def __call__(self,*agrs,**kwargs):

        return ‘hello ’+self.func(*args,**kwagrs)

 

        In front of the original function of the increase @pp field, call the greeting function will output hello name of

    Note: Class A decorative function and decorative use, it is recommended to use the function decorators, as a function of decorator decoration for normal function and class methods can be used, while class class method decorator for decorative troublesome.

    6.5 parameterized decorator

        In a nested function decorator external function to the input parameters.

Guess you like

Origin www.cnblogs.com/zhuome/p/11361654.html