并发
新建少量进程
import multiprocessing
import time
def func(msg):
for i in xrange(3):
print msg
time.sleep(1)
if __name__ == "__main__":
p = multiprocessing.Process(target=func,args=("hello",))
p.start()
p.join()
print "Sub-process done."
进程池
使用apply_async,没有async就是阻塞版本了
import multiprocessing
import time
def func(msg):
for i in xrange(3):
print msg
time.sleep(1)
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=4)
for i in xrange(10):
msg = "hello %d" %(i)
pool.apply_async(func,(msg,))
pool.close()
pool.join()
print "Sub-process(es) done."
使用Pool,并关注结果
import multiprocessing
import time
def func(msg):
for i in xrange(3):
print msg
time.sleep(1)
return "done" + msg
if __name__ == "__main__":
pool = multiprocessing.Pool(processes=4)
result = []
for i in xrange(10):
msg = "hello %d" %(i)
result.append(pool.apply_async(func,(msg,)))
pool.close()
pool.join()
for res in result:
print res.get()
print "Sub-process(es) done."
小结
Python下比较好的并行方式是使用多进程,可以非常有效的使用CPU资源,实现真正意义上的并发。
线程共享相同的地址空间和内存,线程之间通信是非常容易的,但是进程之间通信要复杂一些。进程间通信有管道,消息队列,Socket接口等。