042 multi-process,

Source: Internet
Author: User

042 multi-process,

Content: Process Creation (two methods)

#############################
Method 1: Create a thread object and set the parameters of the method and method for executing the process.

from multiprocessing import Processimport timedef f(name):    time.sleep(0.2)    print('hello',name,time.ctime())if __name__ == '__main__':    p_list = []    for i in range(3):        p = Process(target=f,args=('xxx',))        p_list.append(p)        p.start()    for i in p_list:        i.join()    print('end')

#############################
Method 2:
1. Create a class to inherit the Process class
2. Override the run method in the class.
3. Create an object and execute the start method to enable the thread.

From multiprocessing import Processimport timeclass MyProcess (Process): def _ init _ (self): super (MyProcess, self ). _ init _ () def run (self): time. sleep (0.2) print ('hello', self. name, time. ctime () if _ name _ = '_ main _': p_list = [] for I in range (3): p = MyProcess () p. start () p_list.append (p) for t in p_list: t. join () print ('end') # running result: # hello MyProcess-1 Sun Feb 4 09:19:07 2018 # hello MyProcess-2 Sun Feb 4 09:19:07 2018 # hello MyProcess-3 Sun Feb 4 09:19:07 2018 # end

Self. name is the default start name, which can be changed by yourself.

 

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.