0

I am running a Nastran simulation through Python.

nastran=subprocess.run([nastrandir,fn,"parallel = 4","old=no",outputarg])

These simulations tend to run for quite some time without feedback, so I am trying to automate reading the output file for relevant data and printing it.

To do that, I need to run some code during the subprocess runtime. However, this does not seem to work. As a simple test I wrote this code beneath the subprocess command:

while nastran.poll() is None:
   print("Still working   \r")
   time.sleep(delay)
   print("Still working.  \r")
   time.sleep(delay)
   print("Still working.. \r")
   time.sleep(delay)
   print("Still working...\r")
   time.sleep(delay)

Unfortunately, the code seems to get stuck at the subprocess command and waits for it to finish, at which point nastran becomes a CompletedProcess class and can no longer be polled, which is the error I receive.

Any idea on how I can get Python to properly poll my Nastran subprocess?

Jarn
  • 13
  • 2
  • Create the subprocess yourself via `Popen` and `poll()` the instance. There also no reason to have more than one `time.sleep` / `print` in your loop. – martineau Mar 19 '19 at 16:01

1 Answers1

0

Here is a solution to achieve your goal. While this does not necessarily poll MSC Nastran, this solution does allow you to inspect the output files while MSC Nastran is running.

The solution involves the use of the watchdog library. I use the watchdog library to read the log file during an MSC Nastran run.

Below is a functional example.

import watchdog.events
from watchdog.observers import Observer
import subprocess


# Class defining a scan folder
class ScanFolder:
    def __init__(self, path):
        self.path = path
        self.event_handler = watchdog.events.PatternMatchingEventHandler(patterns=["*.log"],
                                                                         ignore_patterns=[],
                                                                         ignore_directories=True)
        self.event_handler.on_any_event = self.parse_log
        self.observer = Observer()
        self.observer.schedule(self.event_handler, self.path, recursive=False)
        self.observer.start()

    def parse_log(self, event):

        if event.event_type is not 'deleted':
            name_of_file = event.src_path.replace('.\\', '').replace('./', '')

            # The code that reads the LOG file goes here
            print('Change detected in the following file')
            print(name_of_file)

    def stop_actions(self):
        self.observer.stop()


# Start monitoring
path_to_monitor = '.'
scan_folder = ScanFolder(path_to_monitor)

# Run MSC Nastran
nastran_command = '/msc/MSC_Nastran/20181/bin/msc20181'
subprocess.call([nastran_command, 'nastran', 'dsoug1.dat', 'batch=NO'])

print('End of script')
# output
# MSC Nastran V2018.1 (Intel Linux 4.15.0-88-generic) Tue Mar  3 21:01:43 2020
#
# *** SYSTEM INFORMATION MESSAGE (pgm: nastran, fn: estimate_job_requirements)
#     Starting ESTIMATE, please wait...
#
# *** USER INFORMATION MESSAGE (pgm: nastran, fn: estimate_job_requirements)
#     Estimated bpool=7664.4MB
#     Estimated DOF=2
#     Estimated memory=7959.5MB
#     Estimated disk=1.2MB
# MSC Nastran beginning job dsoug1.
# Change detected in the following file
# dsoug1.log
# Change detected in the following file
# dsoug1.log
# Change detected in the following file
# dsoug1.log
# Change detected in the following file
# dsoug1.log
# ...
# Change detected in the following file
# dsoug1.log
# Change detected in the following file
# dsoug1.log
# MSC Nastran job dsoug1 completed.
# End of script
Chris A.
  • 91
  • 5