Python 多处理 - 如何拆分工作负载以提高速度?
Python multiprocessing - How can I split workload to get speed improvement?
我正在编写一个裁剪图像并保存的简单代码。
但是问题是图片数量在150000+左右,想提高速度
所以,一开始我写了一个简单的for循环代码,如下所示:
import cv2
import numpy
import sys
textfile=sys.argv[1]
file_list=open(textfile)
files=file_list.read().split('\n')
idx=0
for eachfile in files:
image=cv2.imread(eachfile)
idx+=1
if image is None:
pass
outName=eachfile.replace('/data','/changed_data')
if image.shape[0]==256:
image1=image[120:170,120:170]
elif image.shape[0]==50:
image1=image
cv2.imwrite(outName,image1)
print idx,outName
此代码处理 90000 张图像大约需要 38 秒。
但是,使用双核比单进程花费更多的时间,同样的 90000 张图像大约需要 48 秒。
import cv2
import sys
import numpy
from multiprocessing import Pool
def crop(eachfile):
image=cv2.imread(eachfile)
idx+=1
if image is None:
pass
outName=eachfile.replace('/data','/changed_data')
if image.shape[0]==256:
image1=image[120:170,120:170]
elif image.shape[0]==50:
image1=image
cv2.imwrite(outName,image1)
print idx,outName
if __name__=='__main__':
textfile=sys.argv[1]
file_list=open(textfile)
files=file_list.read().split('\n')
pool=Pool(2)
pool.map(crop,files)
我在加快流程方面做得对吗?或者我应该拆分列表并将每个列表发送到进程?
任何关于我的代码的评论都会很棒!!!
提前致谢!!!
您确实应该将任务分配给两个核心。尝试使用此示例代码 "slightly modified"。可以找到OPhere。你看到 data
的地方就是你提供图片的钩子。使用 multiprocessing
时,defs 在 class 下不工作...如果您尝试使用 pathos...您会从 cPickle 中得到错误...最新 2.7 版本的一些烦人的问题。不会出现在 3.5 或其他版本中。享受吧!
import multiprocessing
def mp_worker((inputs, the_time)):
print " Process %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
sys.stdout.flush()
def mp_handler(): # Non tandem pair processing
p = multiprocessing.Pool(2)
p.map(mp_worker, data)
def mp_handler_tandem():
subdata = zip(data[0::2], data[1::2])
# print subdata
for task1, task2 in subdata:
p = multiprocessing.Pool(2)
p.map(mp_worker, (task1, task2))
#data = (['a', '1'], ['b', '2'], ['c', '3'], ['d', '4'])
data = (['a', '2'], ['b', '3'], ['c', '1'], ['d', '4'],
['e', '1'], ['f', '2'], ['g', '3'], ['h', '4'])
if __name__ == '__main__':
sys.stdout.flush()
# print 'mp_handler():'
# mp_handler()
# print '---'
# time.sleep(2)
# print '\nmp_handler_tandem():'
# mp_handler_tandem()
print '---'
# time.sleep(2)
Multiprocess().qmp_handler()
working within an editor: use sys.stdout.flush()
to flush your output to screen while it happens.
但还要检查 使用内核和拆分作业。
我正在编写一个裁剪图像并保存的简单代码。
但是问题是图片数量在150000+左右,想提高速度
所以,一开始我写了一个简单的for循环代码,如下所示:
import cv2
import numpy
import sys
textfile=sys.argv[1]
file_list=open(textfile)
files=file_list.read().split('\n')
idx=0
for eachfile in files:
image=cv2.imread(eachfile)
idx+=1
if image is None:
pass
outName=eachfile.replace('/data','/changed_data')
if image.shape[0]==256:
image1=image[120:170,120:170]
elif image.shape[0]==50:
image1=image
cv2.imwrite(outName,image1)
print idx,outName
此代码处理 90000 张图像大约需要 38 秒。 但是,使用双核比单进程花费更多的时间,同样的 90000 张图像大约需要 48 秒。
import cv2
import sys
import numpy
from multiprocessing import Pool
def crop(eachfile):
image=cv2.imread(eachfile)
idx+=1
if image is None:
pass
outName=eachfile.replace('/data','/changed_data')
if image.shape[0]==256:
image1=image[120:170,120:170]
elif image.shape[0]==50:
image1=image
cv2.imwrite(outName,image1)
print idx,outName
if __name__=='__main__':
textfile=sys.argv[1]
file_list=open(textfile)
files=file_list.read().split('\n')
pool=Pool(2)
pool.map(crop,files)
我在加快流程方面做得对吗?或者我应该拆分列表并将每个列表发送到进程?
任何关于我的代码的评论都会很棒!!!
提前致谢!!!
您确实应该将任务分配给两个核心。尝试使用此示例代码 "slightly modified"。可以找到OPhere。你看到 data
的地方就是你提供图片的钩子。使用 multiprocessing
时,defs 在 class 下不工作...如果您尝试使用 pathos...您会从 cPickle 中得到错误...最新 2.7 版本的一些烦人的问题。不会出现在 3.5 或其他版本中。享受吧!
import multiprocessing
def mp_worker((inputs, the_time)):
print " Process %s\tWaiting %s seconds" % (inputs, the_time)
time.sleep(int(the_time))
print " Process %s\tDONE" % inputs
sys.stdout.flush()
def mp_handler(): # Non tandem pair processing
p = multiprocessing.Pool(2)
p.map(mp_worker, data)
def mp_handler_tandem():
subdata = zip(data[0::2], data[1::2])
# print subdata
for task1, task2 in subdata:
p = multiprocessing.Pool(2)
p.map(mp_worker, (task1, task2))
#data = (['a', '1'], ['b', '2'], ['c', '3'], ['d', '4'])
data = (['a', '2'], ['b', '3'], ['c', '1'], ['d', '4'],
['e', '1'], ['f', '2'], ['g', '3'], ['h', '4'])
if __name__ == '__main__':
sys.stdout.flush()
# print 'mp_handler():'
# mp_handler()
# print '---'
# time.sleep(2)
# print '\nmp_handler_tandem():'
# mp_handler_tandem()
print '---'
# time.sleep(2)
Multiprocess().qmp_handler()
working within an editor: use
sys.stdout.flush()
to flush your output to screen while it happens.
但还要检查