用Python尽可能快地打开Powershell文件

2024-09-30 10:28:22 发布

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

当前我正在加载一个Powershell文件,该文件将返回一个值。有没有办法加载得更快?因为我的程序正在打开8.ps1文件,需要10到15秒才能使用。我可以启动程序,然后在程序已经运行时将字符串放入其中吗(因为它是一个Tkinter GUI)

p = subprocess.check_output(["powershell.exe", "C:\\PowershellFiles\\bios_settings.ps1"])
bios = str(p)
if bios == 'b\'[BIOS:config:Network:MACAdressPassThr]disabled\\n\\r\\n\'':
    bios = '[BIOS:config:Network:MACAdressPassThr]\tdisabled'
else:
    bios = '[BIOS:config:Network:MACAdressPassThr]\tenabled'

Tags: 文件字符串程序configtkintercheckguinetwork
2条回答

假设将字符串放入标签中,则可以在创建标签后使用.configure()方法更改标签。因此,您可以创建tkinter GUI并在powershell完成后使用.configure()对其进行更新。如果您使用了任何按钮,可以等待稍后显示或禁用它们,直到powershell命令完成

myLabel.configure(text=bios)

或者可以使用stringVar来更新它

bios = StringVar()
myLabel = Label(root, textvariable=bios)

p = subprocess.check_output(["powershell.exe", "C:\\PowershellFiles\\bios_settings.ps1"])
    bios.set(str(p))
    if bios.get() == 'b\'[BIOS:config:Network:MACAdressPassThr]disabled\\n\\r\\n\'':
        bios.set('[BIOS:config:Network:MACAdressPassThr]\tdisabled')
    else:
        bios.set('[BIOS:config:Network:MACAdressPassThr]\tenabled')

但是,您未处理的字符串会在那里闪烁一瞬间

如果速度更快,您可以尝试:

import os

os.system("cmd /c powershell C:\\PowershellFiles\\bios_settings.ps1")

另外,我相信您的脚本正在按顺序执行powershell中的脚本。我不知道脚本之间是否存在数据依赖性,但如果没有,可以考虑使用线程并发运行所有脚本:

import os
import threading

cmds =  [
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps1",
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps2",
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps3",
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps4",
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps5",
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps6",
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps7",
            "cmd /c powershell C:\\PowershellFiles\\bios_settings.ps8"
        ]

def thread_function(index):
    os.system(cmds[index])

for i in range(len(cmds)):
    x = threading.Thread(target = thread_function, args = (i, ))
    x.start()

相关问题 更多 >

    热门问题