python - 如何在python中一个接一个地运行两个进程

标签 python multithreading python-2.7 asynchronous threadpool

我正在尝试运行两个命令,一个接一个。我的代码如下所示:

baking.bake()
print "baking completed"

我的目标是运行 baking.bake()(大约需要 1 分钟才能完成),然后立即打印“baking started”。最后,烘烤完成后我想打印“烘烤完成”。本质上:如何异步运行 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中一个接一个地运行两个进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24014374/

相关文章:

python - 找出频率最高的组合并添加标签

python - 在Factory Boy中,如何连接Faker创建的字符串?

python - redis-py 属性错误 : 'PubSub' object has no attribute 'get_message'

python - 安装 Python 时出错

python-2.7 - 在 Windows 命令行界面中安装 twilio 时获取 "SyntaxError"

python - 为什么我的返回列表的类不会迭代?

python - pycurl 与谷歌应用引擎

c# - 确保长时间运行的任务只触发一次,后续请求排队但队列中只有一个条目

android - 处理程序是否是线程,具有处理程序和线程的Looper的作用是什么?

Linux 应用程序在 pthread_create() 中崩溃