Python:使用子流程和wine正确筛选

2024-10-16 20:48:59 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我目前正在远程工作,试图在Ubuntu机器上运行Python脚本,同时不维护与它的ssh连接。 通常的解决方案是分离屏幕,允许脚本在断开连接管道的同时运行

然而,我的脚本调用了另一个程序(LTspice),该程序恰好只在Windows上运行,但却是使用Wine安装在Ubuntu机器上的

我正在使用python中的子进程库来执行此调用。(wine LTspice.exe)

尽管ssh连接时脚本工作正常,但一旦管道断开,脚本就无法处理,原因不明,但只要我重新连接并重新连接,脚本就会再次工作。 这个问题可能来自不同的方面,因为它涉及ssh、screen、wine、python子流程和LTspice,但我正在尝试调查和解决这个问题。(我有物理背景,因此请处理我在这些方面的浅薄知识,我想学习)

为了对作为我主脚本一部分的问题进行简单测试,我创建了这个小的其他测试脚本:

Test.py:

from subprocess import call
import subprocess
import os
import sys
import config2 as config # a config file that includes the paths in a Linux format

file_path = config.tmp_asc_file[:-4]
spice_exe_path = config.LTSpice_executable_path
pathC='C:/'+file_path.split('/',5)[-1] #this adapt the path to windows format that LTspice reads 
                                       #inside the wine environment

while True:
    command="wine "+ spice_exe_path+' -wine -netlist "'+ pathC+ '.asc"'
    print(repr(command))
    #call(["bash","-c",command])
    #subprocess.run(['wine',spice_exe_path,'-wine','-netlist',pathC+'.asc' ],check=True)
    subprocess.run(['bash','-c', command],check=True)

    command='wine '+ spice_exe_path+' -wine -b'+' -ascii "'+ pathC+'.net"'
    print(repr(command))
    #call(["bash","-c",command])
    #subprocess.run(['wine',spice_exe_path,'-wine','-b','-ascii',pathC+'.net' ],check=True)
    subprocess.run(['bash','-c', command],check=True)

    try:
        os.remove(file_path+'.net')
    except:
        print("file does not exist")
    try:
        os.remove(file_path+'.log')
    except:
        print("file does not exist")
    try:
        os.remove(file_path+'.op.raw')
    except:
        print("file does not exist")
    try:
        os.remove(file_path+'.raw')
    except:
        print("file does not exist")

这个脚本是根据我对Running Bash commands in Python中的建议的理解创建的。 我只是在linux屏幕上运行这个(python test.py)。文件由LTspice处理,因此可以删除创建的输出文件。分离和重新附着不会使脚本失败。但是,分离、断开ssh连接(分离后简单退出),然后再次连接到远程计算机并重新连接显示“文件不存在”打印在前面的输出行中,而最近的显示脚本正在重新工作

此问题是否由屏幕和子流程交互引起?还是葡萄酒/屏风?或者仅仅是因为葡萄酒的问题? 我渴望获得有关调查和测试问题原因的方法的任何反馈,或者只要解决方案允许我不保持与远程机器的连接,就绕过它

提前谢谢


Tags: pathimport脚本trueosexesshcommand