我正在尝试运行两个命令,一个接一个.我的代码如下:baking.bake()print baking completed我的目标是运行akeing.bake()(大约需要1分钟才能完成),此后立即要打印“开始烘焙”.最后,烘烤完成后,我要打印“烘烤完成”...

我正在尝试运行两个命令,一个接一个.我的代码如下:
baking.bake()
print "baking completed"
我的目标是运行akeing.bake()(大约需要1分钟才能完成),此后立即要打印“开始烘焙”.最后,烘烤完成后,我要打印“烘烤完成”.本质上:如何异步运行bake()?
这是我的backing.py文件
# Bake a texture map
from cgkit.cmds import load, worldObject, listWorld
from cgkit.rmshader import RMMaterial, RMShader
from cgkit.sceneglobals import Globals
def bake():
Globals(
bake = True,
resolution = (512, 512),
pixelsamples = (2,2),
output = "ao_map.tif",
displaymode = "rgba"
)
# Load the model
load("singleSofa.obj")
# Obtain a reference to the model
model = worldObject("small_sofa_dark_grey")
# Set the bake material
mat = RMMaterial(
surface = RMShader(
"bake_ao.sl",
samples = 1000,
)
)
model.setMaterial(mat)
解决方法:
您可以使用multiprocessing module,如下所示:
from multiprocessing import Pool
import time
def something(i):
time.sleep(2)
return i+i
pool = Pool(processes=1)
res = pool.apply_async(something, [2])
print "Started something, waiting..."
# ...
print "Done with something. Result was: %s" % (res.get())
因此,在您的情况下,我们可以执行以下操作:
from multiprocessing import Pool
# Create baking object and so forth.
# ...
pool = Pool(processes=1)
res = pool.apply_async(baking.bake)
print "Baking started"
# Then we do something while we wait...
res.get()
print "Baking done."
沃梦达教程
本文标题为:如何在python中一个接一个地运行两个进程


基础教程推荐
猜你喜欢
- python学习与数据挖掘应知应会的十大终端命令 2023-08-08
- Python图像运算之图像掩膜直方图和HS直方图详解 2022-08-30
- pytorch和tensorflow计算Flops和params的详细过程 2022-08-30
- 【python安装】Windows上安装和创建python开发环境 2023-09-03
- python-带有islice的生成器循环中的内存泄漏 2023-11-11
- python 多进程,多线程,协程 2023-09-04
- Python 网页请求之requests库的使用详解 2022-10-20
- python批处理将图片进行放大实例代码 2023-08-04
- Linux下Python2升级Python3 2023-09-03
- 使用Python解析JSON的实现示例 2023-08-11