9
9
from data_juicer .utils .unittest_utils import DataJuicerTestCaseBase
10
10
11
11
12
+ def run_in_subprocess (cmd ):
13
+ result = subprocess .run (
14
+ cmd ,
15
+ shell = True ,
16
+ capture_output = True ,
17
+ text = True
18
+ )
19
+
20
+ if result .returncode != 0 :
21
+ print (f"Command failed with return code { result .returncode } " )
22
+ print (f"Standard Output: { result .stdout } " )
23
+ print (f"Standard Error: { result .stderr } " )
24
+ raise subprocess .CalledProcessError (result , cmd )
25
+
26
+ return result
27
+
28
+
12
29
class ProcessDataTest (DataJuicerTestCaseBase ):
13
30
14
31
def setUp (self ):
@@ -78,10 +95,13 @@ def setUp(self):
78
95
os .makedirs (self .tmp_dir )
79
96
80
97
def _auto_create_ray_cluster (self ):
81
- if not subprocess . call ( 'ray status' , shell = True ) :
98
+ try :
82
99
# ray cluster already exists, return
100
+ run_in_subprocess ('ray status' )
83
101
self .tmp_ray_cluster = False
84
102
return
103
+ except :
104
+ pass
85
105
86
106
self .tmp_ray_cluster = True
87
107
head_port = '6379'
@@ -95,12 +115,10 @@ def _auto_create_ray_cluster(self):
95
115
96
116
print (f"current rank: { rank } ; execute cmd: { cmd } " )
97
117
98
- result = subprocess .call (cmd , shell = True )
99
- if result != 0 :
100
- raise subprocess .CalledProcessError (result , cmd )
118
+ run_in_subprocess (cmd )
101
119
102
120
def _close_ray_cluster (self ):
103
- subprocess . call ('ray stop' , shell = True )
121
+ run_in_subprocess ('ray stop' )
104
122
105
123
def tearDown (self ):
106
124
super ().tearDown ()
@@ -148,10 +166,8 @@ def test_ray_image(self):
148
166
with open (tmp_yaml_file , 'w' ) as file :
149
167
yaml .dump (yaml_config , file )
150
168
151
- status_code = subprocess .call (
152
- f'python tools/process_data.py --config { tmp_yaml_file } ' , shell = True )
169
+ run_in_subprocess (f'python tools/process_data.py --config { tmp_yaml_file } ' )
153
170
154
- self .assertEqual (status_code , 0 )
155
171
self .assertTrue (osp .exists (tmp_out_path ))
156
172
157
173
import ray
0 commit comments