乐趣区

关于人工智能:Python基础python并发示例

并发

新建大量过程

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 接口等。

退出移动版