Processes are managed by the system itself.
1: The most basic wording
From multiprocessing import pooldef F (x): return x*xif __name__ = = ' __main__ ': p = Pool (5) Print (P.map (f, [1, 2, 3]))
[1, 4, 9]
2. In fact, the process is produced through the Os.fork method.
In Unix, all processes are produced by means of a fork.
Multiprocessing Processosinfo (title): Title, __name__ (OS,):, Os.getppid (), Os.getpid () f (name): info () , name__name__ = =: info () p = Process (=f, = (,)) P.start () P.join ()
3. Thread Shared Memory
Threadingrun (info_list,n): Info_list.append (n) info_list__name__ = =: info=[] I (): p=threading. Thread (=run,=[info,i]) P.start ()
[0] [0, 1] [0, 1, 2] [0, 1, 2, 3] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4, 5] [0, 1, 2, 3, 4, 5, 6] [0, 1, 2, 3, 4, 5, 6, 7] [0, 1, 2, 3, 4, 5, 6, 7, 8] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Process does not share memory:
Multiprocessing Processrun (info_list,n): Info_list.append (n) info_list__name__ = =: info=[] I (): P=Pro Cess (=run,=[info,i]) P.start ()
[1] [2] [3] [0] [4] [5] [6] [7] [8] [9]
If you want to share memory, use the queue in the multiprocessing module
Multiprocessing Process, Queuef (q,n): Q.put ([N,]) __name__ = =: Q=queue () I (): P=process (=f,= (q,i)) P.start (): Q.get ()
4, Lock: Only for screen sharing, because the process is independent, so for multi-process is not used
Multiprocessing Process, LOCKF (L, i): L.acquire (), I l.release () __name__ = =: Lock = lock () num (): Process (=f, = (lock, num)). Start ()
Hello World, 1hello World, 2hello World, 3hello World, World, 4hello World, 5hello World, 6hello World, 7hello World, World 0hello World 9
5, inter-process memory sharing: Value,array
Multiprocessing Process, Value, ARRAYF (N, a): N.value = I ((a)): a[i] =-a[i]__name__ = =: num = Value (,) arr = Array (, ()) Num.value arr[:] p = Process (=f, = (num, arr)) P.start () P.join ()
0.0[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]3.1415927[0,-1, 2,-3, 4,-5,-6,-7,-8,-9]
#manager共享方法, but slow
Multiprocessing Process, Managerf (D, L): d[] = d[] = d[] = l.reverse () __name__ = =: Manager = Manager () d = Manag Er.dict () L = manager.list (()) p = Process (=f, = (d, l)) P.start () P.join () d l
# print '-------------' Here's just another way # print Pool.map (F,range (10))
{0.25:none, 1: ' 1 ', ' 2 ': 2} [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
#异步: It's not much to use.
Multiprocessing POOLTIMEF (x): X*x time.sleep () x*x__name__ = =: Pool=pool (=) res_list=[] I (): RE S=pool.apply_async (F,[i]) res_list.append (res) r Res_list:r.get (timeout=10) #超时时间
01491625360149464981162536496481
Synchronous is the Apply
This article is from the "Walker" blog, please be sure to keep this source http://223228686.blog.51cto.com/2222284/1869862
python-Multi-process