简体   繁体   中英

Why does python stop on subprocess exit?

I'm using Python's subprocess.call() to run aa series of python scripts that each run Matlab scripts. Trouble is, once the first Matlab script ends.

The outer Python script parses a directory of csv files for settings to run experiments based on each line of the csv files. For each experiment, it calls a python program to run parse the data and feed into Matlab. Matlab, then runs each experiment. Except that the entire thing exits after the first time matlab is run. Could Matlab exiting its sub-subprocess bring down the entire thing?

 for line in csvfile:
      if debug:
          print 'Experiment %d' % count

      ts = line.split(',')
      startStamp=ts[0]
      cmdargs = ['python prep_lssvm.py']
      cmdargs.append(str(site))
      cmdargs.append(str(startStamp))
      cmdargs.append(str(daysTraining))

      if debug:
          print cmdargs

      for i in range(len(argv)-2):
          cmdargs.append(str(argv[i+2]))

      command = ' '.join(cmdargs)

      if debug:
          print command

      call(command,shell=True)
      #Never goes past here<<<<<<=======================
      dirname = ''.join([site,'_',str(count)])
      mkdir(dirname)
      call(''.join(['mv ',site,'/*.txt ',dirname]),shell=True,stdout=outfile)

It seems I have solved this through rubber-duck debugging.

I was calling the MATLAB script through Popen(), which executes asynchronously, rather than call(), which executes synchronously. Changing all instances of Popen to call appears to have solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM