在Python 2.5中使用Python 2.6子进程模块
我想使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我知道这个问题已经得到解答,但就其价值而言,我在 Python 2.3 中使用了 Python 2.6 附带的 subprocess.py,并且运行良好。 如果您阅读文件顶部的注释,它会显示:
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:确实没有什么好的方法可以做到这一点。 subprocess 在 python 中实现 (与 C 相对),因此您可以将模块复制到某个地方并使用它(当然希望它不使用任何 2.6 的优点)。
另一方面,您可以简单地实现子进程声称要做的事情,并编写一个在 *nix 上发送 SIGTERM 并在 Windows 上调用 TerminateProcess 的函数。 以下实现已在 Linux 和 Win XP 虚拟机上进行了测试,您需要 python Windows 扩展:
这样你只需要维护
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:
That way you only have to maintain the
terminate
code rather than the entire module.虽然这不能直接回答您的问题,但可能值得了解。
从 __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.我遵循 Kamil Kisiel 关于在 python 2.5 中使用 python 2.6 subprocess.py 的建议,它运行得很好。 为了使它更容易,我创建了一个 distutils 包,您可以 easy_install 和/或将其包含在构建中。
要在 python 2.5 项目中使用 python 2.6 的子进程:
代码中
在buildout 的
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:
in your code
in buildout
以下是一些在 Windows 上结束进程的方法,直接取自
http://code.activestate.com/recipes/347462/
Here are some ways to end processes on Windows, taken directly from
http://code.activestate.com/recipes/347462/
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.