我正在尝试在 celery Worker 中使用 bpy python 包渲染图像。 设置如下
celery_app.py
from celery import Celery
app = Celery('tasks', broker='pyamqp://guest@localhost//', include=['tasks'])
if __name__ == '__main__':
app.start()
主.py
from celery_app import app
if __name__ == '__main__':
print(app.send_task('tasks.render_task', args=[]))
任务.py
import bpy
from os.path import abspath
from celery_app import app
@app.task(bind=True, acks_late=True, reject_on_worker_lost=True)
def render_task(self):
bpy.ops.wm.open_mainfile(filepath="/tmp/test/untitled.blend")
bpy.context.preferences.addons["cycles"].preferences.compute_device_type = "OPTIX"
bpy.context.preferences.addons["cycles"].preferences.get_devices()
for d in bpy.context.preferences.addons["cycles"].preferences.devices:
if d.type != "CPU":
d["use"] = 1
bpy.context.scene.render.filepath = abspath('test.png')
bpy.ops.render.render(write_still=True)
请注意,搅拌器文件“untitled.blend”是默认场景,以“Cycles”作为渲染引擎,以高质量 .exr 作为照明 HDR。
如果我运行“main.py”,渲染就会开始,但在 HDR 更新时挂起。
[2023-07-22 18:07:55,934: INFO/MainProcess] Task tasks.render_task[5238582a-94c2-4a06-bb3f-c9f540b44405] received
Read blend: "/tmp/test/untitled.blend"
Fra:1 Mem:9.28M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Synchronizing object | Cube
Fra:1 Mem:9.28M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Initializing
Fra:1 Mem:9.12M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Waiting for render to start
Fra:1 Mem:9.12M (Peak 9.28M) | Time:00:00.16 | Mem:0.00M, Peak:0.00M | Scene, ViewLayer | Loading render kernels (may take a few minutes the first time)
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Shaders
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Procedurals
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Background
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Camera
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Meshes Flags
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects | Copying Transformations to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects | Applying Static Transformations
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Particle Systems
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Particle Systems | Copying Particles to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Meshes
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Computing attributes
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Copying Attributes to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Geometry BVH Cube 1/1 | Building BVH
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Geometry BVH Cube 1/1 | Building OptiX acceleration structure
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene BVH | Building
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.23 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene BVH | Building OptiX acceleration structure
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Scene BVH | Copying BVH to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Computing normals
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Mesh | Copying Mesh to device
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Objects Flags
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Primitive Offsets
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Images
Fra:1 Mem:9.12M (Peak 23.82M) | Time:00:00.24 | Mem:0.01M, Peak:0.01M | Scene, ViewLayer | Updating Images | Loading HdrSkyCloudy004_HDR_8K.exr
解决此问题的方法是我可以在 render_task 函数内导入 bpy,这样图像就可以在 22 秒内渲染。
然而,一个有趣的观察是,如果我从搅拌器文件中删除 HDR,则无论导入语句放置在何处,图像都会呈现。但如果导入放在顶部则需要 40 秒,而放在函数调用中则只需要 11 秒。
这也仅在选择 Cycles 作为渲染引擎时才会发生。
所以我对发生的事情有点困惑,用 celery 导入 bpy 模块似乎存在问题。 我真的很想避免在每个函数中本地导入 bpy 模块。
如果有人知道如何避免这种情况,我将不胜感激,或者这是一个固有的搅拌机问题,我应该在那里创建一个问题?
当我尝试用芹菜烘烤纹理时,我也遇到了类似的问题。如果我只烘焙一种纹理,那么它就有效。如果我想烤多个,工人就会挂起。我用 strace 查看了该过程,它一直给我以下错误: epoll_ctl(7, EPOLL_CTL_ADD, 17, {EPOLLIN|EPOLLERR|EPOLLHUP, {u32=17, u64=140720308486161}}) = -1 EEXIST (文件存在)
也许搅拌机尝试在渲染或烘焙期间交换文件并覆盖旧文件,而 celery 的权限不允许这样做。但也许这是一个完全不同的问题。