在Python 2.5中使用Python 2.6子进程模块

发布于 2024-07-13 21:11:05 字数 282 浏览 7 评论 0原文

我想使用Python 2.6版本的子进程,因为它允许 Popen.terminate() 函数,但我坚持使用 Python 2.5。 是否有一些相当干净的方法可以在我的 2.5 代码中使用该模块的新版本? 某种from __future__ import subprocess_module

I would like to use Python 2.6's version of subprocess, because it allows the Popen.terminate() function, but I'm stuck with Python 2.5. Is there some reasonably clean way to use the newer version of the module in my 2.5 code? Some sort of from __future__ import subprocess_module?

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

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

发布评论

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

评论(6

风追烟花雨 2024-07-20 21:11:05

我知道这个问题已经得到解答,但就其价值而言,我在 Python 2.3 中使用了 Python 2.6 附带的 subprocess.py,并且运行良好。 如果您阅读文件顶部的注释,它会显示:

# 该模块应与 Python 2.2 保持兼容,请参阅 PEP 291。

I know this question has already been answered, but for what it's worth, I've used the subprocess.py that ships with Python 2.6 in Python 2.3 and it's worked fine. If you read the comments at the top of the file it says:

# This module should remain compatible with Python 2.2, see PEP 291.

风向决定发型 2024-07-20 21:11:05

确实没有什么好的方法可以做到这一点。 subprocess 在 python 中实现 (与 C 相对),因此您可以将模块复制到某个地方并使用它(当然希望它不使用任何 2.6 的优点)。

另一方面,您可以简单地实现子进程声称要做的事情,并编写一个在 *nix 上发送 SIGTERM 并在 Windows 上调用 TerminateProcess 的函数。 以下实现已在 Linux 和 Win XP 虚拟机上进行了测试,您需要 python Windows 扩展:

import sys

def terminate(process):
    """
    Kills a process, useful on 2.5 where subprocess.Popens don't have a 
    terminate method.


    Used here because we're stuck on 2.5 and don't have Popen.terminate 
    goodness.
    """

    def terminate_win(process):
        import win32process
        return win32process.TerminateProcess(process._handle, -1)

    def terminate_nix(process):
        import os
        import signal
        return os.kill(process.pid, signal.SIGTERM)

    terminate_default = terminate_nix

    handlers = {
        "win32": terminate_win, 
        "linux2": terminate_nix
    }

    return handlers.get(sys.platform, terminate_default)(process)

这样你只需要维护terminate代码而不是整个模块。

There isn't really a great way to do it. subprocess is implemented in python (as opposed to C) so you could conceivably copy the module somewhere and use it (hoping of course that it doesn't use any 2.6 goodness).

On the other hand you could simply implement what subprocess claims to do and write a function that sends SIGTERM on *nix and calls TerminateProcess on Windows. The following implementation has been tested on linux and in a Win XP vm, you'll need the python Windows extensions:

import sys

def terminate(process):
    """
    Kills a process, useful on 2.5 where subprocess.Popens don't have a 
    terminate method.


    Used here because we're stuck on 2.5 and don't have Popen.terminate 
    goodness.
    """

    def terminate_win(process):
        import win32process
        return win32process.TerminateProcess(process._handle, -1)

    def terminate_nix(process):
        import os
        import signal
        return os.kill(process.pid, signal.SIGTERM)

    terminate_default = terminate_nix

    handlers = {
        "win32": terminate_win, 
        "linux2": terminate_nix
    }

    return handlers.get(sys.platform, terminate_default)(process)

That way you only have to maintain the terminate code rather than the entire module.

难以启齿的温柔 2024-07-20 21:11:05

虽然这不能直接回答您的问题,但可能值得了解。

从 __future__ 导入实际上只会更改编译器选项,因此虽然它可以将 with 转换为语句或使字符串文字生成 unicode 而不是 strs,但它不能更改 Python 标准中模块的功能和特性图书馆。

While this doesn't directly answer your question, it may be worth knowing.

Imports from __future__ actually only change compiler options, so while it can turn with into a statement or make string literals produce unicodes instead of strs, it can't change the capabilities and features of modules in the Python standard library.

谎言月老 2024-07-20 21:11:05

我遵循 Kamil Kisiel 关于在 python 2.5 中使用 python 2.6 subprocess.py 的建议,它运行得很好。 为了使它更容易,我创建了一个 distutils 包,您可以 easy_install 和/或将其包含在构建中。

要在 python 2.5 项目中使用 python 2.6 的子进程:

easy_install taras.python26

代码中

from taras.python26 import subprocess

在buildout 的

[buildout]
parts = subprocess26

[subprocess26]
recipe = zc.recipe.egg
eggs = taras.python26

I followed Kamil Kisiel suggestion regarding using python 2.6 subprocess.py in python 2.5 and it worked perfectly. To make it easier, I created a distutils package that you can easy_install and/or include in buildout.

To use subprocess from python 2.6 in python 2.5 project:

easy_install taras.python26

in your code

from taras.python26 import subprocess

in buildout

[buildout]
parts = subprocess26

[subprocess26]
recipe = zc.recipe.egg
eggs = taras.python26
谈情不如逗狗 2024-07-20 21:11:05

以下是一些在 Windows 上结束进程的方法,直接取自
http://code.activestate.com/recipes/347462/

# Create a process that won't end on its own
import subprocess
process = subprocess.Popen(['python.exe', '-c', 'while 1: pass'])


# Kill the process using pywin32
import win32api
win32api.TerminateProcess(int(process._handle), -1)


# Kill the process using ctypes
import ctypes
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)


# Kill the proces using pywin32 and pid
import win32api
PROCESS_TERMINATE = 1
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)
win32api.TerminateProcess(handle, -1)
win32api.CloseHandle(handle)


# Kill the proces using ctypes and pid
import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)

Here are some ways to end processes on Windows, taken directly from
http://code.activestate.com/recipes/347462/

# Create a process that won't end on its own
import subprocess
process = subprocess.Popen(['python.exe', '-c', 'while 1: pass'])


# Kill the process using pywin32
import win32api
win32api.TerminateProcess(int(process._handle), -1)


# Kill the process using ctypes
import ctypes
ctypes.windll.kernel32.TerminateProcess(int(process._handle), -1)


# Kill the proces using pywin32 and pid
import win32api
PROCESS_TERMINATE = 1
handle = win32api.OpenProcess(PROCESS_TERMINATE, False, process.pid)
win32api.TerminateProcess(handle, -1)
win32api.CloseHandle(handle)


# Kill the proces using ctypes and pid
import ctypes
PROCESS_TERMINATE = 1
handle = ctypes.windll.kernel32.OpenProcess(PROCESS_TERMINATE, False, process.pid)
ctypes.windll.kernel32.TerminateProcess(handle, -1)
ctypes.windll.kernel32.CloseHandle(handle)
渡你暖光 2024-07-20 21:11:05

Python 是开源的,您可以自由地采用 2.6 中的 pthread 函数并将其移至您自己的代码中,或者将其用作实现您自己的代码的参考。

由于显而易见的原因,没有办法拥有可以导入部分新版本的 Python 混合体。

Well Python is open source, you are free to take that pthread function from 2.6 and move it into your own code or use it as a reference to implement your own.

For reasons that should be obvious there's no way to have a hybrid of Python that can import portions of newer versions.

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