How to run a script with lot of nested loop without crash I have a blender python script that I used to produce a lot of different dataset for my machine learning project. The problem that I have is that I use a lot of nested loop to simulate a lot of different scene. This is my full script

import bpy
import os
import glob
from math import pi

scene = bpy.context.scene
directory = '/home/cgal/testimageSFS/Hasil_blender/'
list_ob = '/home/cgal/3d_scans/*.ply'
lampu = bpy.data.objects["RGB"]
lampu_utama = bpy.data.objects["Lamp"]

counter = 0

rgb = {'r':(1,0,0) ,'g': (0,1,0) ,'b' : (0,0,1)}

for img in glob.glob(list_ob):

    #create face object
    bpy.ops.object.select_all(action='DESELECT')
    bpy.ops.import_mesh.ply(filepath=img)
    obj = bpy.context.selected_objects[0]
    nama = str(counter)
    obj.name = nama

    #modify face
    face = bpy.data.objects[nama]
    face.scale =(0.00001,0.00001,0.00001)
    face.rotation_euler.x = pi/2
    face.location.z = 3.23

    #attach skin
    mat = bpy.data.materials.new(name = "Skin")
    mat.use_nodes = True #Make so it has a node tree

    #Add the vertex color node
    vc = mat.node_tree.nodes.new('ShaderNodeVertexColor')
    #Assign its layer
    vc.layer_name = "Col"
    #Get the shader
    bsdf = mat.node_tree.nodes["Principled BSDF"]
    #Link the vertex color to the shader
    mat.node_tree.links.new( vc.outputs[0], bsdf.inputs[0] )
    face.data.materials.append(mat)
    #take Pic
    for x in range(-10,11,2):
        for y in range(-7,-2,1):
            for z in range(-6,11,2):
                #change center_lamp position
                lampu_utama.location.x = x
                lampu_utama.location.y = y
                lampu_utama.location.z = z
                xs = str(x+10)
                xs = xs.zfill(2)
                zs = str(z+6)
                zs = zs.zfill(2)
                utama_loc =xs + str(y+7) + zs
                for pow in [1000,5000,10000]:
                    #change RGB power
                    lampu.data.energy = pow
                    power = str(pow)
                    power = power.zfill(5)
                    for i in range(-5,6,1):
                        #change RGB light position
                        lampu.location.x = i

                        #take normal picture
                        for ob in scene.objects:
                            if ob.type == 'CAMERA':
                                lampu.hide_render=True
                                bpy.context.scene.camera = ob
                                file = os.path.join(directory,nama+'_'\
                                +'_'+utama_loc+'_'+str(i+5)+'_'+power+'_a' )
                                bpy.context.scene.render.filepath = file
                                bpy.ops.render.render( write_still=True )

                        for n,color in rgb.items():
                            lampu.data.color = color
                            col=n

                            #record RGB
                            for ob in scene.objects:
                                if ob.type == 'CAMERA':
                                    lampu.hide_render = False
                                    bpy.context.scene.camera = ob
                                    file = os.path.join(directory,nama+'_'\
                                    +'_'+utama_loc+'_'+ str(i+5)+'_'+power+'_'+col )
                                    bpy.context.scene.render.filepath = file
                                    bpy.ops.render.render( write_still=True )

    #delete face object
    bpy.data.objects[nama].select_set(True)
    bpy.ops.object.delete()
    counter=counter+1
    break


The problem is I keep having my PC freeze at some point (not fixed sometimes in 6 min sometimes in 7 min) even when I run the script without GUI, this is the command that I used blender myscene.blend --background --python myscript.py. So is there any solution to fix this besides reducing the number of possible scene on my script? Thank you

My PC specs :
ubuntu 18.04
i7 3770
gtx 1080ti
16 GB of RAM


Full blend file



link to one of .ply I used :
download here
Ok so apparently when I run it on same PC but with windows OS it can run without any crash so far. So I will submit this into bug for linux later on. Thank you for all the helpful comment especially lemon who go as far as trying my code in his/her PC