Python in multiprocessing module

Source: Internet
Author: User

Multiprocess module What are you doing here?

A: You can use multiprocessing to create child processes in the main process.

#该模块和Threading模块使用方法基本类似.

    • The first thing you need to explain is that the functions you use for multithreading cannot have return, For example, you will be the job this function multithreading so in the job this function can not have return, if there is a return value see the end of the article.
    • Multiprocessing is best written in if __name__ = = ' __main__ ', Windows may go wrong if it is not placed in this area. This is not the case with the rest of the environment. My first case was not added, and the second case was added.

Case:

1 #!usr/bin/env python2 #Encding:utf-83 #by I3ekr4 5 ImportMultiprocessing,time6 7 defJob (q,a):#将Queue当参数传入, and must be in the first place. 8Time.sleep (3)9     Print 'This is test...%s'%(a)Ten  One  AStart =time.time () -Q =multiprocessing. Queue ()#定义一个Queue, this queue is a must.  -P1 = multiprocessing. Process (target=job,args= (q,1) #创建一个线程, the first q is the queue, and the second is the value of the parameter. If there are no parameters, you must (Q,) the comma must be added  theP2 = multiprocessing. Process (target=job,args= (q,2)) - P1.start () - P2.start () - P1.join () + P2.join () -End =time.time () + Print("run time is%s"% (End-start))

Output Result:

[Email protected]:/home/i3ekr/desktop# python 1.py
This is test ... 1
This is test ... 2
Run Time is 3.0163371563

So what do you do when the function has a return value?

Use the queue. Put () to save the value. Use get to get the return value. As shown in the following example:

It should be noted that:

    • How many return values will have a get. It's not like all the return values are output all at once. The following code is 20-21 lines, because there are two threads, so two times the job function is executed, so there are two return values. It is also necessary to have two get to get value
1 #!usr/bin/env python2 #Encding:utf-83 #by I3ekr4 5 ImportMultiprocessing,time6 7 defJob (q,a):8Time.sleep (3)9Q.put (a)#get aTen  One  A if __name__=='__main__': -Start =time.time () -Q =multiprocessing. Queue () theP1 = multiprocessing. Process (target=job,args= (q,1)) -P2 = multiprocessing. Process (target=job,args= (q,2)) - P1.start () - P2.start () + P1.join () - P2.join () +Res1 = Q.get ()#Get job return value ARes2 =Q.get () at     PrintRes1,res2 -End =time.time () -     Print("run time is%s"% (End-start))

Output Result:

[email protected]:/home/i3ekr/desktop# python 1.py
1 2
Run Time is 3.01186394691

Python in multiprocessing module

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.