我想通过python程序调用一个进程,但是,这个进程需要一些由另一个进程设置的特定环境变量。如何获取第一个进程环境变量并将其传递给第二个进程?
这就是程序的样子:
import subprocess
subprocess.call(['proc1']) # this set env. variables for proc2
subprocess.call(['proc2']) # this must have env. variables set by proc1 to work
但是进程不共享相同的环境。请注意,这些程序不是我的(第一个是又大又丑的 .bat 文件,第二个是专有软件),所以我无法修改它们(好吧,我可以从 .bat 中提取我需要的所有内容,但这非常麻烦).
注意:我使用的是 Windows,但我更喜欢跨平台解决方案(但我的问题不会发生在类 Unix 上......)
以下示例说明了如何从批处理或 cmd 文件中提取环境变量,而无需创建包装脚本。享受吧。
from __future__ import print_function
import sys
import subprocess
import itertools
def validate_pair(ob):
try:
if not (len(ob) == 2):
print("Unexpected result:", ob, file=sys.stderr)
raise ValueError
except:
return False
return True
def consume(iter):
try:
while True: next(iter)
except StopIteration:
pass
def get_environment_from_batch_command(env_cmd, initial=None):
"""
Take a command (either a single command or list of arguments)
and return the environment created after running that command.
Note that if the command must be a batch file or .cmd file, or the
changes to the environment will not be captured.
If initial is supplied, it is used as the initial environment passed
to the child process.
"""
if not isinstance(env_cmd, (list, tuple)):
env_cmd = [env_cmd]
# construct the command that will alter the environment
env_cmd = subprocess.list2cmdline(env_cmd)
# create a tag so we can tell in the output when the proc is done
tag = 'Done running command'
# construct a cmd.exe command to do accomplish this
cmd = 'cmd.exe /s /c "{env_cmd} && echo "{tag}" && set"'.format(**vars())
# launch the process
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=initial)
# parse the output sent to stdout
lines = proc.stdout
# consume whatever output occurs until the tag is reached
consume(itertools.takewhile(lambda l: tag not in l, lines))
# define a way to handle each KEY=VALUE line
handle_line = lambda l: l.rstrip().split('=',1)
# parse key/values into pairs
pairs = map(handle_line, lines)
# make sure the pairs are valid
valid_pairs = filter(validate_pair, pairs)
# construct a dictionary of the pairs
result = dict(valid_pairs)
# let the process finish
proc.communicate()
return result
因此,为了回答您的问题,您将创建一个执行以下操作的 .py 文件:
env = get_environment_from_batch_command('proc1')
subprocess.Popen('proc2', env=env)
正如你所说,进程不共享环境 - 所以你字面意义上的要求是不可能的,不仅在Python中,而且在任何编程语言中都是不可能的。
您可以做的是将环境变量放入文件或管道中,或者
后者需要proc2的配合;前者要求在 proc2 启动之前了解变量。
由于您显然使用的是 Windows,因此您需要 Windows 答案。
创建一个包装批处理文件,例如。 “run_program.bat”,并运行这两个程序:
@echo off
call proc1.bat
proc2
脚本将运行并设置其环境变量。两个脚本都在同一个解释器(cmd.exe 实例)中运行,因此变量 prog1.bat 设置 will 在执行 prog2 时设置。
不是很漂亮,但可以用。
(Unix 用户,您可以在 bash 脚本中执行相同的操作:“source file.sh”。)
您可以使用
psutil中的
Process
来获取该进程的环境变量。
如果你想自己实现,可以参考psutil的内部实现。它适应一些操作系统。
当前支持的操作系统有:
例如:在Linux平台下,你可以在
/proc/7877/environ
文件中找到一个pid 7877的环境变量,只需用rt
模式打开即可读取。
当然,最好的方法是:
import os
from typing import Dict
from psutil import Process
process = Process(pid=os.getpid())
process_env: Dict = process.environ()
print(process_env)
您可以在源代码中找到其他平台实现
希望我能帮助你。
Python 标准模块 multiprocessing 有一个队列系统,允许您传递可腌制的对象以通过进程传递。进程还可以使用 os.pipe 交换消息(腌制对象)。请记住,资源(例如:数据库连接)和句柄(例如:文件句柄)不能被 pickle。
您可能会发现这个链接很有趣: 多处理进程之间的通信
关于多处理的 PyMOTw 也值得一提: 多处理基础知识
抱歉我的拼写错误
我想到了两件事:(1)通过将进程以某种方式组合到同一个进程中,使它们共享相同的环境,或者(2)让第一个进程生成包含相关环境变量的输出,这样 Python 就可以读取它并构建第二个进程的环境。我认为(尽管我不是 100% 确定)没有任何方法可以像您希望的那样从子进程中获取环境。
详细阐述 Jason R. Coombs 的答案,我需要一些可以在 unix 系统上运行的东西,并且我让它也可以在 Windows 上运行,因此它是可移植的(在 Windows 10 和 AIX7.2 上测试):
import subprocess
import os
def get_environment_from_batch_command(script):
def validate_pair(ob):
if not (len(ob) == 2):
return False
return True
handle_line = lambda l: l.rstrip().split("=", 1)
if os.name == "nt":
command = f'cmd.exe /E:ON /V:ON /s /c "{script} > NUL && set"'
else:
command = f". {script} > /dev/null 2>&1; printenv"
print(command)
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
shell=True,
)
stdout = p.communicate()[0]
stdout_lines = stdout.decode("utf-8").split("\n")
pairs = map(handle_line, stdout_lines)
valid_pairs = filter(validate_pair, pairs)
result = dict(valid_pairs)
return result
使用示例:
environment = shell.get_environment_from_batch_command(script)
p = subprocess.Popen(
commands,
env=environment,
)
cp = subprocess.run(
commands,
env=environment,
)
环境继承自父进程。在主脚本中设置您需要的环境,而不是子进程(子进程)。