`
schi
  • 浏览: 205186 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

multiprocessing Mesh python script

阅读更多
lukeiamyourfather在http://resources.realflow.com上面共享了他的Multithreaded Mesh Proces脚本,这个脚本是用来实现realflow的多线程输出mesh,虽然realflow的nogui模式有"-threads"这个参数,但你会发现即使你指定了"n"个线程,也是没什么不同的,不管是版4还是版本5,lukeiamyourfather的这个脚本很好的解决了这个问题,他把真正的多线程引入到了realflow的mesh输出。
但是他这个脚本是基于python 3.x来写的,你必须使用python 3.x,不然会出错,因为他用到的一些模块在3.x版本里改名了,如果需要用2.6来运行必须将名称改回来。
参考他的脚本,我使用multiprocessing模块也写了一个,multiprocessing在2.6时被归入了标准库,这样3.x版本应该也能用(我没装,所以没试过),不同的是我这个脚本开始的输出信息可能会有点乱,而他的不会(他加了很多锁,我一个也没),其它的基本一致,我试过对比两个脚本的速度,很接近。
"""
#####################################
##                                 ##
##  multiprocessingMesh v0.1       ##
##  Author: Mack Stone             ##
##  Date: 2011/01/22               ##
##  Email: schistone@gmail.com     ##
##  blog: http://schi.iteye.com  ##
##                                 ##
#####################################
"""
import multiprocessing
import time
import subprocess
import sys, os

# realflow install path
# realflow安装路径,你可能要改它
realflowRootPath = r'C:\Program Files\Next Limit\RealFlow 5'

# print time info
# 输出时间信息
timeInfo = lambda *args: str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))

# Dump subprocess output to /dev/null to keep the terminal window output clean
# 为了让命令行窗口显得干净点
outPut = open('NUL')

# multiprocess class
# 我要输出我想要的信息,所以重写Process
class MeshProcess(multiprocessing.Process):
    def __init__(self, func, args, name=''):
        super(MeshProcess, self).__init__()
        self.name = name
        self.func = func
        self.args = args
        
    def run(self):
        print '>%s -->' % timeInfo(), 'Starting', self.name
        self.res = self.func(*self.args)
        print '>>%s -->' % timeInfo(), self.name, 'exited'
        
# start realflow to process mesh
# 使用realflow进行mesh
def processMesh(q, scene, thread):
    while not q.empty():
        startFrame, endFrame = q.get()
        print '>%s -->' % timeInfo(), 'Thread %d starting meshing frame %d - %d' % (thread, startFrame, endFrame)
        subprocess.Popen('realflow.exe -nogui -threads 1 -mesh -range %s %s "%s"' % (startFrame, endFrame, scene), 
                         stdout=outPut, stderr=outPut, shell=True, cwd=realflowRootPath).wait()
        print '>%s -->' % timeInfo(), 'Thread %d finished meshing frame %d - %d' % (thread, startFrame, endFrame)
    
def main(args):
    helpText = r"""#####################################
##                                 ##
##  multiprocessingMesh v0.1       ##
##  Author: Mack Stone             ##
##  Date: 2011/01/22               ##
##  Email: schistone@gmail.com     ##
##  blog: http://schi.iteye.com  ##
##                                 ##
#####################################

usage: C:\Python26\python.exe multiprocessingMesh.py startFrame endFrame threadCount sceneFile
Example: C:\Python26\python.exe multiprocessingMesh.py 0 100 8 C:\test\test.flw
"""
    # if argv not right print out help
    # 输出帮助信息
    if len(args) < 4 or not os.path.isfile(args[3]):
        print helpText
        return 0
    
    startFrame = int(args[0])
    endFrame = int(args[1])
    threads = int(args[2])
    sceneFile = r'%s' % args[3]
        
    # Populate frameList for each threads
    # 计算帧列表
    frameList = []
    incr = (endFrame + 1) / (threads * 2) + 1
    startList = [i for i in range(startFrame, endFrame+1, incr)]
    endList = [i-1 for i in range(startFrame, endFrame+1, incr)]
    endList.append(endFrame)
    endList.pop(0)
    frameList = zip(startList, endList)
    
    process = []
    q = multiprocessing.Queue()    # 创建列队
    
    print '>%s -->' % timeInfo(), "Begin meshing process..."
    print '>>...'
    # create threads
    # 创建线程
    for i in range(threads):
        x = i + 1
        p = MeshProcess(processMesh, (q, sceneFile, x), 
                        "Thread %d" % x)
        process.append(p)
        
    # start threads
    # 线程开始
    for i in range(threads):
        process[i].start()
        
    # Fill the queue
    # 将帧列表放到列队中排队
    for f in frameList:
        q.put(f)
        
    # end threads
    # 线程任务完成,结束线程
    for i in range(threads):
        process[i].join()
        
    print '>>>...'
    print '>>%s -->' % timeInfo(), "End of meshing process"
    
    # exit
    # 退出
    sys.exit()
    
if __name__ == '__main__':
    main(sys.argv[1:])

使用视频
http://u.115.com/file/f455884023#
multiprocessingMesh.mp4

希望对你们有帮助
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics