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: [email protected] ##
## 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: [email protected] ##
## 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:])

使用視頻
[url]http://u.115.com/file/f455884023#[/url]
multiprocessingMesh.mp4

希望對你們有幫助
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章