Some knowledge of Python

Source: Internet
Author: User
Tags iterable object serialization

1, data collection:

Set

Class set ([Iterable]) Features: Data duplication is not allowed, duplicate data is deleted, data is not sorted


List

Class list ([Iterable]) features: Allow data duplication

Tuple:

such as A= (a) allow data duplication, but after the tuple initialization, do not add, change the value of operations, the data will not be changed

Dictionary:

such as a={"1": "One"} key should not be repeated, otherwise, key will only point to the last value

2, the serialization of data:

First, open the input/output stream by opening (), and get a Stream object F

Input:

Pickle (DATA,F)//object serialization

Output:

Pickle (f)//The deserialization of objects

3, Multi-process:

In a Linux system, Python can easily create sub-processes through the OS module,

Pid=os.fork () Creates a child process based on the original process, and thereafter the code has the parent process and the child process execution.

The fork () call returns two times at a time, returns the ID of the child process in the parent process, 0 is returned in the subprocess, the child process obtains the parent process ID through Getppid (), and obtains its own ID through Getpid ().

Windows systems, by encapsulating the multiprocessing module, by providing a process class to create child processes,

def fun (name):

Print (name)

Form multiprocessing Import Process

P=process (target=fun,args= (' Bob ',))

P.start ()

P.join ()

For join () it is necessary to explain the join ([timeout]), with the default parameter, in seconds, after calling join (), the parent process will wait for the child process to complete the task lock before execution, if the parameter is set on the join (), the child process will be locked after the specified time, and then the parent process executes. Otherwise, the parent process waits for Python to lock the child process before it can continue execution.

Process Pools Pool ():

Class Multiprocessing.pool.Pool ([processes[, initializer[, initargs[, maxtasksperchild[, context]] []]

The first parameter, processes: refers to the number of simultaneous processes, and if you do not specify a parameter, Python defaults to the number of cores in the computer

interprocess communication, done through Queue,pipes, simply refer to the

4, Multiple threads:

Multithreading has two implementations, one is the common inheritance of the threading class, and then the replication run () method, the second is to call the Threading.thread () construction method, the function to be executed as a parameter passed in

class Threading. Thread (group=none, target=none, name=none, args= (), kwargs={}, *,daemon=none) Call the Start () method again, the thread still has the join () method, its usage is similar to the process, when the child thread calls join (), the main thread will stop executing the following code, Wait for the child thread to complete the task lock and then execute. If not called, the child thread executes in parallel with the main thread.


The difference between multi-process and multi-threading: In multi-process, a variable, each process will itself copy a copy, do not affect each other, and in multiple threads, each thread is common one variable, so, this time, need to pay extra attention to the concurrency of the thread. When necessary, only one thread can be allowed to manipulate variables at the same time


Now probably remember all the basic knowledge, there is a mistake to welcome the point that it is already hungry and dizzy head.




Some knowledge of Python

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.