1.多进程(执行效率与CPU核数直接相关)
from multiprocessing import Pool, Manager
import os, time, random
#多进程执行的任务
def task(m_list):
print('Run task pid %s...' % ( os.getpid()))
for i in range(5):
m_list.append(i)
print(m_list)
if __name__=='__main__':
start_time = time.time()
print('Parent process %s.' % os.getpid())
p = Pool()
#Manager支持的类型有list,dict,Namespace,Lock,RLock,Semaphore,BoundedSemaphore,Condition,Event,Queue,Value和Array
m = Manager() # 创建共享list
m_list = m.list()
# for i in range(multiprocessing.cpu_count()): # 根据CPU核数创建进程
for i in range(5):
p.apply_async(task, args=(m_list,)) # 注意:args中若为单个变量,必须以逗号','结尾
print('Waiting for all subprocesses done...')
p.close()
p.join()
print('All subprocesses done.')
end_time = time.time()
print('use time: {0}'.format(end_time - start_time))
2.多线程(python中的多线程由于GIL的原因,并不会提升效率)
import threading
import time
#线程
class ThreadImpl(threading.Thread):
def __init__(self, num):
threading.Thread.__init__(self)
self._num = num
def run(self):
global total, mutex
# 打印线程名
print(threading.currentThread().getName())
# 取得锁
mutex.acquire()
for x in range(0, int(self._num)):
total = total + 1
time.sleep(0.01)
print(total)
# 释放锁
mutex.release()
if __name__ == '__main__':
#定义全局变量
global total, mutex
total = 0
# 创建锁
mutex = threading.Lock()
#定义线程池
threads = []
# 创建线程对象
for x in range(4):
threads.append(ThreadImpl(10))
# 启动线程
for t in threads:
t.start()
# 等待子线程结束
for t in threads:
t.join()
# 打印执行结果
print('total: {0}'.format(total))