易于使用的python子进程接口。

EasyProcess的Python项目详细描述


easyprocess是一个易于使用的python子进程接口。

链接:

TravisCoverallsLatest VersionSupported Python versionsLicenseCode HealthDocumentation

功能:
  • 位于subprocess模块顶部的层
  • 易于启动和停止程序
  • 易于获得标准输出/错误,返回程序代码
  • 命令可以是list(首选)或string(使用shlex.split将命令字符串转换为list)
  • 日志记录
  • 超时
  • 单元测试
  • 跨平台,在Linux上开发
  • 不支持shell
  • 不支持管道
  • stdout/stderr仅在子流程完成后设置
  • stop()不会杀死整个子进程树
  • Unicode支持
  • 支持的Python版本:2.7、3.4、3.5、3.6、3.7
  • 方法chaining
类似项目:

基本用法

>>> from easyprocess import EasyProcess
>>> EasyProcess('python --version').call().stderr
u'Python 2.6.6'

安装

安装:

pip install EasyProcess

卸载:

pip uninstall EasyProcess

用法

简单示例

示例程序:

#-- include('examples/hello.py')--#
from easyprocess import EasyProcess
import sys

s = EasyProcess([sys.executable, '-c', 'print "hello"']).call().stdout
print(s)
#-#

输出:

#-- sh('python -m easyprocess.examples.hello')--#
hello
#-#

一般

命令可以是字符串列表或串联字符串:

#-- include('examples/cmd.py')--#
from easyprocess import EasyProcess

print('-- Run program, wait for it to complete, get stdout (command is string):')
s=EasyProcess('python -c "print 3"').call().stdout
print(s)

print('-- Run program, wait for it to complete, get stdout (command is list):')
s=EasyProcess(['python','-c','print 3']).call().stdout
print(s)

print('-- Run program, wait for it to complete, get stderr:')
s=EasyProcess('python --version').call().stderr
print(s)

print('-- Run program, wait for it to complete, get return code:')
s=EasyProcess('python --version').call().return_code
print(s)

print('-- Run program, wait 1 second, stop it, get stdout:')
s=EasyProcess('ping localhost').start().sleep(1).stop().stdout
print(s)

#-#

输出:

#-- sh('python -m easyprocess.examples.cmd')--#
-- Run program, wait for it to complete, get stdout (command is string):
3
-- Run program, wait for it to complete, get stdout (command is list):
3
-- Run program, wait for it to complete, get stderr:
Python 2.7.6
-- Run program, wait for it to complete, get return code:
0
-- Run program, wait 1 second, stop it, get stdout:
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.034 ms
#-#

shell命令

不支持shell命令。

警告

echo是windows上的shell命令(没有echo.exe)。 但它是一个Linux上的程序。

返回代码

easyprocess.return_code在 调用easyprocess.stop或easyprocess.wait。

通过将语句一起使用,启动进程 并自动停止:

from easyprocess import EasyProcess
with EasyProcess('ping 127.0.0.1') as proc: # start()
    # communicate with proc
    pass
# stopped

等同于:

from easyprocess import EasyProcess
proc = EasyProcess('ping 127.0.0.1').start()
try:
    # communicate with proc
    pass
finally:
    proc.stop()

超时

这是用“守护线程”实现的。

“整个Python程序退出时只剩下守护进程线程。” http://docs.python.org/library/threading.html

#-- include('examples/timeout.py')--#
from easyprocess import EasyProcess

s = EasyProcess('ping localhost').call(timeout=2).stdout
print(s)
#-#

输出:

#-- sh('python -m easyprocess.examples.timeout')--#
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.025 ms
#-#
< H3>替换现有函数

更换操作系统:

retcode = os.system("ls -l")
==>
p = EasyProcess("ls -l").call()
retcode = p.return_code
print(p.stdout)

更换子流程。调用:

retcode = subprocess.call(["ls", "-l"])
==>
p = EasyProcess(["ls", "-l"]).call()
retcode = p.return_code
print(p.stdout)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何显示因用户而异的SQLite存储数据?   转换java。sql。将映射传递给Jackon的valueToTree方法时StringNode的时间戳   从java中的列表json获取值   unicode Java字符存储在什么编码中?   java如何让Spring数据存储库中的默认方法命中缓存?   java使用readClassDescriptor()和resolveClass()来允许序列化版本控制   数组通过另一个矩阵的一部分填充矩阵   如果包含使用正则表达式的字符串,则替换父XML标记的java   java清除SharedReference中的单个变量   java将变量值从一个jsp页面传输到另一个jsp页面   java JDBC+SQLite:DriveManager不加载所需的驱动程序   相同源代码的java Kotlin构建生成不同的二进制文件   Java中的元组枚举