Python子进程。带有变量设置的Popen

时间:2019-07-15 14:11:30

标签: python

我的一个朋友给我写了一个Python脚本,该脚本使用subprocess.Popen用酒启动Photoshop。

#!/usr/bin/env python3

import subprocess, re, sys, signal

error_keywords = re.compile('^.*(Assertion|0x65372a0).*$')
success_keywords = re.compile('^.*(list_manager_QueryInterface).*$')

exited = False
process = None
successful_launch = False
timeout = 3

def kill_photoshop(signalnum, frame):
    if not successful_launch:
        print("No successful launch withing %d seconds, killing photoshop ..."%timeout, file=sys.stderr)
        process.kill()

signal.signal(signal.SIGALRM, kill_photoshop)

while not exited:
    process = subprocess.Popen(["wine64", "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe"], stderr=subprocess.PIPE)
    successful_launch = False
    signal.alarm(timeout)
    while True:
        if process.poll():
            break
        line = process.stderr.readline()
        # print("got line %d and process %s"%(len(line),process.poll()))
        if len(line) == 0 and process.poll() is not None:
            if process.poll() == 0:
                exited = True
            break
        if line:
            print(line.strip())
            if success_keywords.match(str(line)):
                print("Successful photoshop launch detected", file=sys.stderr)
                successful_launch = True
            if error_keywords.match(str(line)):
                print("Error keyword match, killing process", file=sys.stderr)
                process.kill()
                break
    print("Process return code %d"%process.wait())

由于几天以来已修复一个错误,因此在wine启动命令中添加了一个设置。旧行:

wine64 "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe"

还有新的:

__GL_MaxFramesAllowed="1" wine64 "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe"

因此,我必须在过程中添加__GL_MaxFramesAllowed="1"。我尝试在脚本中添加变量:

my_env = __GL_MaxFramesAllowed="1"

我尝试过

process = subprocess.Popen(["wine64", "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe"], env=my_env, stderr=subprocess.PIPE)

但是不起作用。如何解决?

3 个答案:

答案 0 :(得分:3)

import docker import json from time import sleep client = docker.from_env() container = client.containers.run("my_container", "my", detach=True) while True: container.reload() if not container.attrs['State']['Running']: print(container.logs()) break sleep(1) 的{​​{1}}参数必须是映射(即字典)。

此外,您可能需要复制当前环境并将其添加到其中:

env

答案 1 :(得分:0)

您是否可以使用相同的命令从CLI手动启动?如果没有,我建议先使它工作,然后将其转换为Python。

答案 2 :(得分:0)

另一种选择是执行env program,即您的启动行将是:

PHOTOSHOP = "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe"

process = subprocess.Popen(
  ["env", "__GL_MaxFramesAllowed=1", "wine64", PHOTOSHOP],
  stderr=subprocess.PIPE)

但是在Python中本地执行可能会更好!