Multi-course Process

Source: Internet
Author: User

Multi-process Legacy notation
From multiprocessing import pooldef F (x):    return x*xif __name__ = = ' __main__ ':    p = Pool (5)    print (P.map (f,[ ]))

  

A new style of multi-process
From multiprocessing import processdef run (num):    print "This is", numfor I in range:    t = Process (Target=run,ar Gs= (i,))    T.start ()

  

multi-process-parent-child process

From multiprocessing import processimport osdef info (title):        print title        print ' Module name: ', __name__        if Hasattr (OS, ' Getppid '):                print ' parent process: ', os.getppid ()    #获取父进程PIDprint ' process ID: ', os.getpid ()                 # Get child process Piddef F (name):        info (' function f ')  #子进程调用info函数print ' Hello ', nameif __name__ = = ' __main__ ':        info (' Main Line ')   #父进程调用info函数print '---------------' p = Process (target=f,args= (' Bob ',))        P.start ()        P.join ( )

  

Process data sharing

From multiprocessing import Processli = []def run (num):    li.append (num)    print ' Say hi ', lifor i in range:    t = Process (target=run,args= (i,))    T.start () print ' ending ', Li

  

The process holds one copy of the data, and the data is not shared by default, so that data can be shared between processes .

# method One, Array
From multiprocessing import process,array# creates an array/list that contains only numeric types, and the number of immutable temp = array (' i ', [11,22,33,44]) def Foo (i):    Temp[i] = 100+i for    item in temp:        print I, '-----> ', itemfor I in range (2):    p = Process (target=foo,args= (i,) )    P.start ()

  

# Method Two: manage.dict () sharing 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 ()

  

# Method 3 , Queue
From multiprocessing import process,queuedef F (q,n):    q.put ([N, ' hello ']) if __name__ = = ' __main__ ':    q = Queue () For    I in range (5):        p = Process (target=f,args= (q,i))        P.start () while    True:        print Q.get ()

  

#方法4, Manager
From multiprocessing import process,managerdef F (d,l):    d[1] = ' 1 ' d[' 2 '] = 2d[0.25] = Nonel.reverse ()     #反向列表if __na me__ = = ' __main__ ':    Manager = Manager ()    d = manager.dict ()    #d = {}l = manager.list (range))    #l = [0,1,2, 4,5,6,7,8,9]p = Process (target=f,args= (d,l))    P.start ()    p.join ()    print D    print L

  

Process Lock
From multiprocessing import Process, Array, Rlockdef Foo (lock,temp,i): "" "    Add No. 0 Number" "" "    Lock.acquire ()      #加锁temp [0] = 100+i for    item in temp:        print I, '-----> ', Item    lock.release ()      #解锁lock = Rlock () temp = Array (' i ', [one, one,,]) for I in range:    p = Process (target=foo,args= (Lock,temp,i,))    P.start ()

  

Process Pool

A process sequence is maintained internally by the process pool, and when used, a process is fetched in the process pool, and the program waits until a process is available in the process pool sequence if there are no incoming processes available for use.

There are two methods in a process pool:

    • Apply
    • Apply_async
From  multiprocessing import process,poolimport timedef Foo (i):    time.sleep (2)    return i+100def Bar (ARG):    Print Argpool = Pool (5) #print pool.apply (Foo, (1,)) #print Pool.apply_async (func =foo, args= (1,)). Get () for I in range (Ten):    Pool.apply_async (Func=foo, args= (i,), callback=bar) print ' End ' Pool.close () pool.join () #进程池中进程执行完毕后再关闭, If the comment, then the program closes directly.

  

From multiprocessing import Poolimport timedef f (x):    print x*x    #time. Sleep (2) return x*xif __name__ = ' __main__ ' :    pool = Pool (processes=2)   #同时5个进程res_list = [] for    I in range (5):        res = Pool.apply_async (f,[i,])        # res =  Process (target=f,args=[i,]) print '-----------', I        res_list.append (res) for    R in Res_list:        print ' res_list: ', R.get ()

  


Co-process

The operation of the thread and process is triggered by the program to trigger the system interface, the final performer is the system, and the operation of the coprocessor is the programmer.

The significance of the existence of the process: for multi-threaded applications, the CPU by slicing the way to switch between threads of execution, thread switching takes time (save state, next continue). , only one thread is used, and a code block execution order is specified in one thread.

Application scenario: When there are a large number of operations in the program that do not require the CPU (IO), it is suitable for the association process;

Greenlet

#!/usr/bin/env python#-*-coding:utf-8-*-from  greenlet import Greenlet  def test1 ():    print    Gr2.switch ()    print    gr2.switch ()  def test2 ():    print    gr1.switch ()    Print Gr1 = Greenlet (test1) GR2 = Greenlet (test2) Gr1.switch ()

  

gevent

Import Gevent def foo ():    print (' Running in foo ')    gevent.sleep (0)    print (' Explicit context switch to Foo again ') def Bar ():    print (' Explicit context to bar ')    gevent.sleep (0)    print (' implicit context switch back to bar ') g Event.joinall ([    gevent.spawn (foo),    gevent.spawn (bar),])

  

Automatic switching of IO operation encountered:

from gevent import Monkey; Monkey.patch_all () Import geventimport urllib2def f (URL):    print (' GET:%s '% URL)    resp = urllib2.urlopen (URL)    data = Resp.read ()    print ('%d bytes received from%s. '% (len (data), URL)) gevent.joinall ([        gevent.spawn (F, ' https://www.python.org/'),        gevent.spawn (F, ' https://www.yahoo.com/'),        gevent.spawn (F, ' https://github.com/'),])

  

Multi-course Process

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.