这是实现您的目标的解决方案。虽然这不一定轮询 MSC Nastran,但此解决方案确实允许您在 MSC Nastran 运行时检查输出文件。
该解决方案涉及使用看门狗库。我在 MSC Nastran 运行期间使用看门狗库来读取日志文件。
下面是一个功能示例。
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