• 进程


     1 #!/usr/bin/env python
     2 #coding:utf-8
     3 
     4 from multiprocessing import Pool
     5 
     6 def f(x):
     7     return x*x
     8 
     9 if __name__ == '__main__':
    10     p = Pool(5)
    11     print(p.map(f, [1, 2, 3]))
    12 
    13 
    14 #直接起了三个进程,多进程
     子进程与父进程的关系
    1
    #!/usr/bin/env python 2 #coding:utf-8 3 4 from multiprocessing import Process 5 import os 6 7 def info(title): 8 print title 9 print 'module name:', __name__ 10 if hasattr(os, 'getppid'): # only available on Unix 11 print 'parent process:', os.getppid() 12 print 'process id:', os.getpid() 13 14 def f(name): 15 info('function f') 16 print 'hello', name 17 18 if __name__ == '__main__': 19 info('main line')#此时是父进程 20 p = Process(target=f, args=('bob',))#这边是调用的子进程 21 p.start() 22 p.join()
    进程锁
    1
    #!/usr/bin/env python 2 #coding:utf-8 3 4 from multiprocessing import Process, Lock 5 6 def f(l, i): 7 l.acquire() 8 print 'hello world', i 9 l.release() 10 11 if __name__ == '__main__': 12 lock = Lock() 13 14 for num in range(10): 15 Process(target=f, args=(lock, num)).start()

    进程之间的通信

     1 #!/usr/bin/env python
     2 #coding:utf-8
     3 
     4 '''
     5 from multiprocessing import Process, Queue#这个Queue跟直接import Queue不一样
     6 
     7 def f(q):
     8     q.put([42, None, 'hello'])
     9 
    10 if __name__ == '__main__':
    11     q = Queue()
    12     p = Process(target=f, args=(q,))
    13     p.start()
    14     print q.get()    # prints "[42, None, 'hello']"
    15     p.join()
    16 
    17 
    18 #共享数据
    19 
    20 '''
    21 
    22 from multiprocessing import Process,Queue
    23 
    24 
    25 def f(q,n):
    26     q.put([n,'hello'])
    27 
    28 
    29 if __name__ == '__main__':
    30     q = Queue()
    31     for i in range(5):
    32         p = Process(target=f,args=(q,i))
    33         p.start()
    34 
    35     while True:
    36         print q.get()
    37 
    38 
    39 #将数据都保存一个一个队列中(Queue)

    进程之间内存共享value,array

     1 #!/usr/bin/env python
     2 #coding:utf-8
     3 
     4 from multiprocessing import Process, Value, Array
     5 
     6 def f(n, a):
     7     n.value = 3.1415927
     8     for i in range(len(a)):
     9         a[i] = -a[i]
    10 
    11 if __name__ == '__main__':
    12     num = Value('d', 0.0)
    13     arr = Array('i', range(10))
    14 
    15     p = Process(target=f, args=(num, arr))
    16     p.start()
    17     p.join()
    18 
    19     print num.value
    20     print arr[:]
    21 
    22 
    23 #数据和列表级别的数据共享

    使用较多的内存共享

     1 #!/usr/bin/env python
     2 #coding:utf-8
     3 
     4 from multiprocessing import Process, Manager
     5 
     6 def f(d, l):
     7     d[1] = '1'
     8     d['2'] = 2
     9     d[0.25] = None
    10     l.reverse()
    11 
    12 if __name__ == '__main__':
    13     manager = Manager()
    14 
    15     d = manager.dict()
    16     l = manager.list(range(10))
    17 
    18     p = Process(target=f, args=(d, l))
    19     p.start()
    20     p.join()
    21 
    22     print d
    23     print l
    24 
    25 
    26 #进程之间的内存共享不是真正的内存共享,是通过一个中间通道来进行共享,线程才是真正的共享,support types list, dict, Namespace, Lock, RLock, Semaphore, BoundedSemaphore, Condition, Event, Queue, Value and Array
  • 相关阅读:
    c++11强制转化类型
    关于parent指针以及对话框属性
    关于connect函数(Qt)
    lambda表达式
    学习过程中要注意的一些问题
    自动类型转换
    gakki
    排序算法(2)——简单选择排序和直接插入排序
    [Leetcode]414. Third Maximum Number
    [Leetcode]283. Move Zeroes
  • 原文地址:https://www.cnblogs.com/bill2014/p/6919798.html
Copyright © 2020-2023  润新知