7. Multi-process development

Source: Internet
Author: User

<--Directory--

1. Multi-process


Multi-Process Official documentation

Https://docs.python.org/2/library/multiprocessing.html



"Multi-process"

Example 1:

#

!/usr/bin/env python#coding:utf-8from multiprocessing import pooldef f (x):     return x*x                 #返回自乘值if  __name__ ==  ' __main__ ':     p = pool (5)                #定义5个进程      print  (P.map (f,[1,2,3]))   #调用f函数, send 1,2.3 to X, let them squared Example 2: (This script can only be used under Linux to produce effects) #多进程使用方法: #!/usr/bin/ env python#coding:utf-8from multiprocessing import processimport osimport  Timedef info (title):    print title    print  ' module  name: ', __name__    if hasattr (os,  ' getppid '):         print  ' parent process: ',  os.getppid ()      # Output parent Process Id    time.sleeP (3)     print  ' Process id: ',  os.getpid ()                 #输出子进程IDdef  f (name):     info (' Function f ')     print  ' hellow ',nameif __name__ ==  ' __main__ ':     info (' main line ')     print  '---------------'      p = process (target=f,args= (' Bob ',))     p.start ()      p.join ()


Usage: How many cores the CPU uses, or 1 time times the number of CPU cores

Multi-process use and multi-threaded use of different points (this script can only run under Linux) [[email protected] opt]# cat pro.py #!/usr/bin/env python#coding:utf-8from Multiprocessing Import Processimport threadingdef run (info_list,n): Info_list.append (n) Print info_listinfo=[]for I In range: p = Process (Target=run,args=[info,i]) #多进程独享内存, 10 process P.start () print '---------threding-----' info=[ ]for i in range: P = Threading. Thread (Target=run,args=[info,i]) #多线程共享内存, 10 Threads P.start ()



Memory synchronization method between processes: (memory sharing first method)

[Email protected] opt]# vim multiprocess.py #!/usr/bin/env python#coding:utf-8from multiprocessing Import Process,queu Edef f (q,n): Q.put ([N, ' hello ']) print q.get () print q.get () if __name__ = = ' __main__ ': q = Queue () q.put (' DD d ') for I in range (5): p = Process (target=f,args= (q,i)) P.start () while True:print Q.get ()


The second method of memory sharing

#!/usr/bin/env python#coding:utf-8from multiprocessing Import Process, Value, Arraydef f (N, a): N.value = 3.1415927 For I in range (Len (a)): a[i] =-a[i]if __name__ = = ' __main__ ': num = Value (' d ', 0.0) arr = Array (' I ', range (1 0)) p = Process (target=f, args= (num, arr)) P.start () p.join () Print num.value print arr[:]


Third method of memory sharing (recommended, more supported types)

#!/usr/bin/env python#coding:utf-8from multiprocessing Import process,managerdef F (d,l): d[1] = ' 1 ' #给字典赋值 d[2 ] = ' 2 ' d[0.25] = None l.reverse #把列表反向输出if __name__ = = ' __main__ ': Manger = Manager () d = manger.dict ( ) #获取字典, empty dictionary L = manger.list (range) #获取列表 p = Process (Target=f, args= (d,l)) P.start () P.join () print D Print L


Multi-process through pool, process pool (not commonly used)

#!/usr/bin/env python#coding:utf-8from multiprocessing Import poolimport timedef f (x): Print x*x time.sleep (2) Re    Turn X*xpool = Pool (processes=5) #每次跑5个进程res_list = []for i in range: res = Pool.apply_async (f, [I,]) #定义十个进程 #res = Process (Target=f, Args[i,]) #上面那一句相当于这一句的意思 #print '----: ', I res_list.append (res) for R in RES_LIST:PR int R.get () #print Pool.map (F.range (10))


#进程生成线程

#!/usr/bin/env python#coding:utf-8from multiprocessing import poolimport timedef  f (x):    info_list = []    for i in  Range () 5:        t = threading. Thread (Target =t_func,args=[info_list,i])         t.start () pool  = pool (processes=5)    #每次跑5个进程res_list  = []for i in range (10):     res = pool.apply_async (F, [i,])    #定义十个进程       #res  = process (Target=f, args[i,])    #上面那一句相当于这一句的意思      print  '----: ', I    res_list.append (res) for r in res_list:        print r.get (timeout=1) #print  pool.map (F.range (10)) 1, creating a process program from  Multiprocessing import processimport threadingimPort timedef foo (i):    print  ' Say hi ', Ifor i in range ( ):     p = process (target=foo,args= (i,))     p.start ()


2. Data sharing between processes


The default holds one copy of each



Copy Code

#!/usr/bin/env python#coding:utf-8from multiprocessing Import processfrom multiprocessing import Managerimport Timeli =    []def Foo (i): Li.append (i) print ' Say hi ', Li for I in range: p = Process (target=foo,args= (i,)) P.start () Print Li


Data sharing between processes can be accomplished through a special data structure



Copy Code

#方法一, arrayfrom multiprocessing Import process,arraytemp = Array (' i ', [11,22,33,44]) def Foo (i): temp[i] = 100+i for I Tem in Temp:print I, '-----> ', itemfor I in range (2): P = Process (target=foo,args= (i,)) P.start () P.join ()


#方法二: Manage.dict () shared data

From multiprocessing Import process,managermanage = Manager () dic = Manage.dict () def Foo (i): dic[i] = 100+i print dic . VALUES () for I in Range (2): P = Process (target=foo,args= (i,)) P.start () P.join ()


#方法三: Manage. Namespace () Sharing data

From multiprocessing Import process,managermanage = Manager () namespace = manage.  Namespace () namespace.x = [11,22,33]def Foo (i,dic): namespace.x = [11,22,33,i] Print namespacefor i in range (2): P = Process (target=foo,args= (i,namespace)) P.start () P.join ()


This article is from the "Wsyht90 blog" blog, make sure to keep this source http://wsyht90.blog.51cto.com/9014030/1843519

7. Multi-process development

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.