参考:
https://blog.csdn.net/ruanxingzi123/article/details/83024727
使用subprocess库
test1.py
import cv2
import os
import sys
DATASET = sys.argv[1]
os.environ['CUDA_VISIBLE_DEVICES'] = sys.argv[2]
print(DATASET)
print(os.environ['CUDA_VISIBLE_DEVICES'])
img = cv2.imread("../data/mydata/img/001.png", -1)
H,W = img.shape[:2]
center = (int(W/2), int(H/2))
center_offset = (int(W/2)+100, int(H/2)+100)
cv2.rectangle(img, center, center_offset, (0,255,0), 4)
content = 'test1'
cv2.putText(img, content, center_offset,
cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2)
cv2.imwrite("./result_test1.png", img)
test2.py
import cv2
import os
import sys
DATASET = sys.argv[1]
os.environ['CUDA_VISIBLE_DEVICES'] = sys.argv[2]
print(DATASET)
print(os.environ['CUDA_VISIBLE_DEVICES'])
img = cv2.imread("./result_test1.png", -1)
H,W = img.shape[:2]
center = (int(W/2), int(H/2))
center_offset = (int(W/2)+200, int(H/2)+200)
cv2.rectangle(img, center, center_offset, (0,255,0), 4)
content = 'test2'
cv2.putText(img, content, center_offset,
cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2)
cv2.imwrite("./result_test2.png", img)
test3.py
import cv2
import os
import sys
DATASET = sys.argv[1]
os.environ['CUDA_VISIBLE_DEVICES'] = sys.argv[2]
print(DATASET)
print(os.environ['CUDA_VISIBLE_DEVICES'])
img = cv2.imread("./result_test2.png", -1)
H,W = img.shape[:2]
center = (int(W/2), int(H/2))
center_offset = (int(W/2)+300, int(H/2)+300)
cv2.rectangle(img, center, center_offset, (0,255,0), 4)
content = 'test3'
cv2.putText(img, content, center_offset,
cv2.FONT_HERSHEY_SIMPLEX, 2, (0,0,255), 2)
cv2.imwrite("./result_test3.png", img)
test_process.py
import subprocess
python_path = "/home/***/.conda/envs/py3_6--pytorch1_1_0/bin/python"
A = subprocess.run(python_path + " test1.py mydata 0", shell=True, check=True)
B = subprocess.run(python_path + " test2.py mydata 2", shell=True, check=True)
C = subprocess.run(python_path + " test3.py mydata 1", shell=True, check=True)
test_shell.sh
#!/usr/bin/env bash
#PYTHON=/root/train/***/anaconda3_py3.7/bin/python
PYTHON=/home/***/.conda/envs/py3_6--pytorch1_1_0/bin/python
nohup $PYTHON test_process.py > test_result.log 2>&1 &