Manually set Number of Threads for Cloth Bake


Is there any way to have lower level access over CPU usage during cloth bakes through the Python API? I’d like to be able to manually set the number of threads to use.

I am running distributed computing on Linux machines. I am running two seperate tasks.

Let’s say N = Number of Threads on Machine

Task 1 : Blender Cloth Bake

  • This uses N threads. The average thread uses about 30% CPU.

Task 2 : Python Single Threaded program.

  • This uses 1 thread at 100% CPU.

If I run these at the same time, I get some bottleneck issues. In the above scenario I want is for Blender to only use N-1 threads while 1 thread is only running my Python function.

I would prefer not to setup VM’s to do this.

This isn’t linux, but here’s how to do this in Windows:
Hopefully this will help you do the research for linux. I’ll poke around a little more and update this if I find anything.

So you’d set blender to use CPU’s 0 to N-1 and Python to only use N. Note that Blender couldn’t use processor N for any purpose.

Update: If you search for “Linux Processor Affinity” you’ll find articles about how to do this in Linux, however it’s not as easy. You’ll have to be comfortable doing a lot of command line stuff that I don’t understand.

You might have better luck asking around on AskUbuntu or a similar Linux forum.

Thanks for the input. I’m going to put some time into this, I just wonder if in Blender code they are specifying the number of threads to use. I’m worried that even setting Linux Processor Affinity wont change the number of threads spawned within Blender code.

Setting the thread affinity manually is not a great way to manage this, since blender by default will still start the number of threads base on the number of cores detected, if you artificially restrict the affinity of those threads to fewer cores, you’ll get excessive context switches and reduced performance.

Blender has a command line option to limit the number of threads, but once started it cannot be changed through the python api afaik.

-t or --threads <threads>
	Use amount of <threads> for rendering and other operations
	[1-1024], 0 for systems processor count.
1 Like

I am running Blender by importing bpy within Python. There are a lot of things I need to do in Blender as Python module before baking and/or rendering.

I also do multiple bakes within the same Python workflow. For example, I might bake for 100 frames, then do something in python, and then add new cloth modifier and bake for another 100 frames or so.

I could do everything in Blender as Python module, save the Blend file and then use python to send shell script similar to blender “myscene.blend -t 8 --background --python” or something like that. But this would add some serious complexity to the workflow. It would be harder to track crashes, when the bake has finished, etc…

Is there any way to set number of threads when building Blender as Python module? Or does anyone know if this is something that could be considered adding to Python API?