如何从子进程获取环境?

发布于 2024-07-29 13:32:03 字数 468 浏览 6 评论 0原文

我想通过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

但是 to 进程不共享相同的环境。 请注意,这些程序不是我的(第一个是又大又丑的 .bat 文件,第二个是专有软件),所以我无法修改它们(好吧,我可以从 .bat 中提取我需要的所有内容,但这非常麻烦)。

注意:我使用的是 Windows,但我更喜欢跨平台解决方案(但我的问题不会发生在类 Unix 上......)

I want to call a process via a python program, however, this process need some specific environment variables that are set by another process. How can I get the first process environment variables to pass them to the second?

This is what the program look like:

import subprocess

subprocess.call(['proc1']) # this set env. variables for proc2
subprocess.call(['proc2']) # this must have env. variables set by proc1 to work

but the to process don't share the same environment. Note that these programs aren't mine (the first is big and ugly .bat file and the second a proprietary soft) so I can't modify them (ok, I can extract all that I need from the .bat but it's very combersome).

N.B.: I am using Windows, but I prefer a cross-platform solution (but my problem wouldn't happen on a Unix-like ...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

傲鸠 2024-08-05 13:32:04

由于您显然使用的是 Windows,因此您需要 Windows 答案。

创建一个包装批处理文件,例如 “run_program.bat”,并运行这两个程序:

@echo off
call proc1.bat
proc2

脚本将运行并设置其环境变量。 两个脚本都在同一个解释器(cmd.exe 实例)中运行,因此 prog1.bat 设置的变量将在执行 prog2 时设置。

不是很漂亮,但它会起作用。

(Unix 用户,您可以在 bash 脚本中执行相同的操作:“source file.sh”。)

Since you're apparently in Windows, you need a Windows answer.

Create a wrapper batch file, eg. "run_program.bat", and run both programs:

@echo off
call proc1.bat
proc2

The script will run and set its environment variables. Both scripts run in the same interpreter (cmd.exe instance), so the variables prog1.bat sets will be set when prog2 is executed.

Not terribly pretty, but it'll work.

(Unix people, you can do the same thing in a bash script: "source file.sh".)

方圜几里 2024-08-05 13:32:04

您可以使用 psutil 中的 Process 来获取环境变量对于该过程。

如果你想自己实现,可以参考psutil的内部实现。 它适应一些操作系统。

目前支持的操作系统有:

  • AIX
  • FreeBSD、OpenBSD、NetBSD
  • Linux
  • macOS
  • Sun Solaris
  • Windows

例如:在 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)

您可以在 源代码中找到其他平台实现

希望我能帮助你。

You can use Process in psutil to get the environment variables for that Process.

If you want to implement it yourself, you can refer to the internal implementation of psutil. It adapts to some operating system.

Currently supported operating systems are:

  • AIX
  • FreeBSD, OpenBSD, NetBSD
  • Linux
  • macOS
  • Sun Solaris
  • Windows

Eg: In Linux platform, you can find one pid 7877 environment variables in file /proc/7877/environ, just open with rt mode to read it.

Of course the best way to do this is to:

import os
from typing import Dict
from psutil import Process

process = Process(pid=os.getpid())
process_env: Dict = process.environ()

print(process_env)

You can find other platform implementation in source code

Hope I can help you.

禾厶谷欠 2024-08-05 13:32:04

Python 标准模块多处理 有一个队列系统,允许您传递可腌制的对象要通过流程。 进程还可以使用 os.pipe 交换消息(腌制对象)。 请记住,资源(例如:数据库连接)和句柄(例如:文件句柄)不能被腌制。

您可能会发现这个链接很有趣:
多处理进程之间的通信

另外值得一提的是关于多处理的 PyMOTw :
多处理基础

抱歉我的拼写

The Python standard module multiprocessing have a Queues system that allow you to pass pickle-able object to be passed through processes. Also processes can exchange messages (a pickled object) using os.pipe. Remember that resources (e.g : database connection) and handle (e.g : file handles) can't be pickled.

You may find this link interesting :
Communication between processes with multiprocessing

Also the PyMOTw about multiprocessing worth mentioning :
multiprocessing Basics

sorry for my spelling

叶落知秋 2024-08-05 13:32:04

我想到了两件事:(1) 通过以某种方式将它们组合到同一个进程中,使进程共享相同的环境,或者 (2) 让第一个进程生成包含相关环境变量的输出,这样 Python 就可以读取它并构建第二个进程的环境。 我认为(尽管我不是 100% 确定)没有任何方法可以像您希望的那样从子进程中获取环境。

Two things spring to mind: (1) make the processes share the same environment, by combining them somehow into the same process, or (2) have the first process produce output that contains the relevant environment variables, that way Python can read it and construct the environment for the second process. I think (though I'm not 100% sure) that there isn't any way to get the environment from a subprocess as you're hoping to do.

凉宸 2024-08-05 13:32:04

详细阐述 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 = get_environment_from_batch_command(script)
    p = subprocess.Popen(
        commands,
        env=environment,
    )
    cp = subprocess.run(
        commands,
        env=environment,
    )

elaborating on Jason R. Coombs answers, I needed something that would work on unix system, and I made it working also for Windows, so it's portable (tested on Windows 10 and 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

Examples to use it:

    environment = get_environment_from_batch_command(script)
    p = subprocess.Popen(
        commands,
        env=environment,
    )
    cp = subprocess.run(
        commands,
        env=environment,
    )
腻橙味 2024-08-05 13:32:04

我参考了Rafiki的答案,在value中添加了支持识别换行符和=字符的功能。 它适用于 centos/ubuntu 和 windows10。

def getEnvsFromEnvsScript(script_path: str, current_exec_envs: dict) -> dict:
    def setValue(env_dict: dict, key: str, value: list) -> None:
        value[-1] = value[-1].rstrip()
        env_dict[key] = ''.join(value)
    if os.name == "nt":
        command = f'cmd.exe /E:ON /V:ON /s /c "chcp 65001 >nul&&"{script_path}" > nul&& set"'
    else:
        command = f'. "{script_path}" > /dev/null 2>&1; printenv'
    ret = subprocess.run(
        command,
        env=current_exec_envs,
        text=True,
        shell=True,
        capture_output=True
    )
    # print(command)
    # print(ret.stdout)
    env_dict, key, value = {}, None, []
    for line in ret.stdout.splitlines(True):
        if '=' in line:
            if key is not None:
                setValue(env_dict, key, value)
                value = []
            # split on first `=`
            key, val = line.split('=', 1)
            # remove space char
            key = key.strip()
            line = val
        value.append(line)
    if key is not None:
        setValue(env_dict, key, value)
        value = []
    return env_dict

I refered the answer of Rafiki and added the feature of supporting recognization of newline characters and = character in value. It works on centos/ubuntu and windows10.

def getEnvsFromEnvsScript(script_path: str, current_exec_envs: dict) -> dict:
    def setValue(env_dict: dict, key: str, value: list) -> None:
        value[-1] = value[-1].rstrip()
        env_dict[key] = ''.join(value)
    if os.name == "nt":
        command = f'cmd.exe /E:ON /V:ON /s /c "chcp 65001 >nul&&"{script_path}" > nul&& set"'
    else:
        command = f'. "{script_path}" > /dev/null 2>&1; printenv'
    ret = subprocess.run(
        command,
        env=current_exec_envs,
        text=True,
        shell=True,
        capture_output=True
    )
    # print(command)
    # print(ret.stdout)
    env_dict, key, value = {}, None, []
    for line in ret.stdout.splitlines(True):
        if '=' in line:
            if key is not None:
                setValue(env_dict, key, value)
                value = []
            # split on first `=`
            key, val = line.split('=', 1)
            # remove space char
            key = key.strip()
            line = val
        value.append(line)
    if key is not None:
        setValue(env_dict, key, value)
        value = []
    return env_dict
偷得浮生 2024-08-05 13:32:04

环境是从父进程继承的。 在主脚本中设置您需要的环境,而不是在子进程(子进程)中。

Environment is inherited from the parent process. Set the environment you need in the main script, not a subprocess (child).

无名指的心愿 2024-08-05 13:32:03

以下示例说明了如何从批处理或 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)

Here's an example of how you can extract environment variables from a batch or cmd file without creating a wrapper script. Enjoy.

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

So to answer your question, you would create a .py file that does the following:

env = get_environment_from_batch_command('proc1')
subprocess.Popen('proc2', env=env)
童话里做英雄 2024-08-05 13:32:03

正如你所说,进程不共享环境 - 所以你字面意义上的要求是不可能的,不仅在Python中,而且在任何编程语言中都是不可能的。

您可以做的是将环境变量放入文件或管道中,然后

  • 让父进程读取它们,并在创建 proc2 之前将它们传递给 proc2,或者
  • 让 proc2 读取它们,并在本地设置它们,

后者需要 proc2 的配合; 前者要求在 proc2 启动之前了解变量。

As you say, processes don't share the environment - so what you literally ask is not possible, not only in Python, but with any programming language.

What you can do is to put the environment variables in a file, or in a pipe, and either

  • have the parent process read them, and pass them to proc2 before proc2 is created, or
  • have proc2 read them, and set them locally

The latter would require cooperation from proc2; the former requires that the variables become known before proc2 is started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文