[Python] Running two functions in their own processes and terminating one

Discussão em 'Python' iniciado por Stack, Fevereiro 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a setup to run a render and measure the workload on the GPU in Python, using two functions that I run independently using the multiprocessing module to launch them. However, I need to terminate one from the other, so I store the object returned by subprocess.popen() globally to terminate it:

    import os
    import multiprocessing
    import subprocess

    nvidia_cmd = None

    blender_cmd_to_run = '/home/mickey/dev/blender-4.2.1-linux-x64/blender -b /home/mickey/dev/elastic_shared_memory/benchmark_scenes/bmw27/bmw27.blend -f 10 -- --cycles-device CUDA'
    nvidia_cmd_to_run = 'nvidia-smi --query-gpu=gpu_bus_id,memory.used --format=csv -l 1'

    def run_blender():
    blender_cmd = subprocess.Popen(blender_cmd_to_run, shell=True, stdout=subprocess.PIPE)
    for line in blender_cmd.stdout:
    print(line.decode().strip())
    nvidia_cmd.terminate()

    def run_nvidia():
    global nvidia_cmd
    nvidia_cmd = subprocess.Popen(nvidia_cmd_to_run, shell=True, stdout=subprocess.PIPE)
    for line in nvidia_cmd.stdout:
    print(line.decode().strip())


    if __name__ == '__main__':
    blender_process = multiprocessing.Process(target=run_blender)
    nvidia_process = multiprocessing.Process(target=run_nvidia)

    nvidia_process.start()
    blender_process.start()


    However, when the blender_process function ends, I get an error with the terminate call:

    AttributeError: 'NoneType' object has no attribute 'terminate'


    But why? I'm storing the value as soon as the other command is launched. What am I missing?

    Continue reading...

Compartilhe esta Página