Python Learning--python Encoding
Write this blog to motivate yourself and share your experiences and issues with others.
First, the process
1. Overview
The multiprocessing package is a multi-process Management Pack in Python. With Thread. threading, you can use the multiprocessing object to create a process. The processing object has the same usage as the Thread object, and there is a method for start () run () join (). See the code below for specific use. There are a few things to note when using these APIs:
- It is necessary to call the join () method on each process object to prevent the process from becoming a tool for the zombie process. This is not required in multi-threading because there is only one process.
- No data can be shared between two processes, and if you want to share data, you need to have a third party to communicate the two to share. Use Pipe and Queue as a priority, and avoid using lock/event/semaphone/condition and other synchronization methods.
- Under Windows system, to start a child process, you must add If __name__ = = "__main__":, the process related to write under this sentence.
1 #Author: "LFD"2 #DATE:2018/6/83 fromMultiprocessingImportProcess4 Import Time5 6 deff (name):7Time.sleep (1)8 Print('Hello', Name,time.ctime ())9 Ten if __name__=="__main__": OneP_list = [] A forIinchRange (3): -p = Process (target=f,args= ('Liufeiduo',))#Create a process - p_list.append (P) the P.start () - forPinchp_list: - P.join () - + Print('Jincheng end!') - + " "# Run Result: A Hello Liufeiduo Fri June 8 11:15:46 2018 at Hello Liufeiduo Fri June 8 11:15:46 2018 - Hello Liufeiduo Fri June 8 11:15:46 2018 - Jincheng end! - " "
creat jincheng_1
Above is through the method to achieve multi-process;
The following is a multi-process implementation by calling the class;
1 fromMultiprocessingImportProcess2 Import Time3 4 classmyprocess (Process):5 def __init__(self,name):6Super (Myprocess,self).__init__()7Self.name =name8 defRun (self):9Time.sleep (1)Ten Print('Hello', Self.name,time.ctime ()) One A if __name__=='__main__': -P_list = [] - forIinchRange (3): thep = myprocess ('Liufeiduo') - P.start () - p_list.append (P) - + forPinchp_list: - P.join () + A Print('End')
creat jincheng_2
2. Process Relationships
1 fromMultiprocessingImportProcess2 ImportOs,time3 4 definfo (title):5 Print(title)6 Print('Module Name:',__name__)7 Print('Parent Process:', Os.getppid ())8 Print('Process ID:', Os.getpid ())9 Ten deff (name): One Pass A - if __name__=='__main__': -Info'\033[32;1mmain Process line\033[0m') theTime.sleep (5) -p = Process (target=info,args= ('Bob',)) - P.start () - P.join () + - + A " "Operation Result: at Main process line - Module Name: __main__ - The parent process:7708 7708 is the process ID that Pycharm has on the computer - Process id:13972 - Bob - Module Name: __mp_main__ in Parent process:13972 - Process id:6024 to + " "
Jincheng Relationship
Ii. coding of Python2 and Python3
Code: The basic concept is simple. First, we start with a piece of information, the message, that the message exists in a human understandable and understandable sense. I intend to call this expression "Ming
Text "(plain text). For people who speak English, the English words printed on the paper or displayed on the screen count as clear text.
Second, we need to be able to turn the plaintext message into some other representation, and we need to be able to turn the encoded text back into plaintext. Conversions from plaintext to encoded text are called "encodings", from the encoding
The text is then returned to Cheng Mingwen for "decoding."
Python 3 Learning--python Multi-process and encoding