I think the best way is to directly specify with the CUDA_VISIBLE_DEVICES environment variable.
Something like
CUDA_VISIBLE_DEVICES=1,2 python myscript.py

so your script will only see GPUs number 1 and 2, and won’t touch the other GPUs