Django stuck after running Docker command as subprocess

When I run this command:

manim_command = f'sudo docker run --rm -v {base_dir}/manim/python_code_files:/mnt/code -v {base_dir}/media:/mnt/output manim-image manim -ql /mnt/code/ -o /mnt/output/{class_name}'

using or subprocess.Popen or os.system, It successfully runs(create an image or video file), but it blocks the terminal and prevents the further rendering of the template.

If the file is already present, it will successfully render the template. But if the file is not present, it will create the file, but doesn't render the template.

I have tried running the docker container in detached mode, but that just renders the template before creating the file. obviously we have to wait till the process is over:

    while True:
        if check_file_exists(class_name):
    print('File created')    

so, it waits till the file is created, but when I add this block, it waits for the the file to be created, prints 'File created' and then gets stuck just before rendering the template.

why doesn't it render?

            # Run the function
            print('function completed')
        except Exception as e:
            result_message = f"Error executing shell command: {e}"

        #after POST request
        context = {'previous_code': previous_code,
                   'MEDIA_URL': settings.MEDIA_URL,
                   'placeholder': False,
        return render(request, 'run.html',context)    

It even prints 'function completed'

I have tried:

  • using thread to run as an asynchronous process but the same result as with running in detached mode
  • stopping and removing the container. It seems that the container is already stopped because I have used --rm. so, what is blocking?
  • killing the subprocess with process.kill() and process.terminate(). These commands ran without error, but did not unblock the terminal.
  • running with os.system instead of subprocess.
  • running without shell= True, same result

Back to Top