我正在通过Python运行Nastran模拟。
nastran=subprocess.run([nastrandir,fn,"parallel = 4","old=no",outputarg])
这些模拟往往会运行很长时间而没有反馈,因此我试图自动读取输出文件中的相关数据并打印出来。
为此,我需要在子流程运行时中运行一些代码。但是,这似乎不起作用。作为一个简单的测试,我在subprocess
命令下编写了以下代码:
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)
不幸的是,该代码似乎卡在了subprocess
命令中,并等待其完成,此时nastran
变成了CompletedProcess
类,无法再被轮询了,是我收到的错误。
关于如何让Python正确轮询Nastran子进程的任何想法吗?
答案 0 :(得分:0)
这是达成目标的解决方案。尽管这不一定轮询MSC Nastran,但此解决方案确实允许您在MSC Nastran运行时检查输出文件。
该解决方案涉及使用 watchdog 库。我使用看门狗库在MSC Nastran运行期间读取日志文件。
下面是一个功能示例。
ds.repeat(ds.shuffle(buffer_size=number_of_sample).batch(batch_size))