Subprocesses for Humans 2.0.
项目描述
Delegator.py — Subprocesses for Humans 2.0
=======================================
.. image:: https://img.shields.io/pypi/v/delegator.py.svg
:target: https://pypi.python.org/pypi/delegator.py
.. image:: https://img.shields.io/pypi/l/delegator.py.svg
:target: https://pypi.python.org/pypi/delegator.py
.. image:: https://img.shields.io/pypi/wheel/delegator.py.svg
:target: https://pypi.python.org/pypi/delegator.py
.. image:: https://img.shields.io/pypi/pyversions/delegator.py.svg
:target: https://pypi.python.org/pypi/delegator.py
.. image:: https://img.shields.io/badge/SayThanks.io-☼-1EAEDB.svg
:target: https://saythanks.io/to/kennethreitz
**Delegator.py**是一个简单的库,用于处理子进程,受
`envoy <https://github.com/kennethreitz/envoy>`_ 和 `pexpect <http://pexpect.readthedocs.io>`_(实际上,它依赖于它!)的启发。
本模块具有两个主要功能 ``delegator.run()`` 和 ``delegator.chain()``。一个用于运行命令,可以是阻塞或非阻塞的,另一个用于运行命令链,命令之间通过标准的Unix管道操作符 ``|`` 分隔。
如果您有兴趣为Kenneth Reitz的开源项目提供经济支持,请考虑访问此链接 <https://cash.me/$KennethReitz>_。您的支持对保持我的动力至关重要,因为开源已不再是我的日常工作的一部分。
基本用法
-----------
基本运行功能
.. code:: pycon
>>> c = delegator.run('ls')
>>> print c.out
README.rst delegator.py
>>> c = delegator.run('long-running-process', block=False)
>>> c.pid
35199
>>> c.block()
>>> c.return_code
0
命令也可以作为列表传入(例如,``['ls', '-lrt']``),用于参数化。
基本链功能
.. code:: pycon
# 也可以使用([[ 'fortune'], ['cowsay']]) 调用。
# 或者,delegator.run('fortune').pipe('cowsay')
>>> c = delegator.chain('fortune | cowsay')
>>> print c.out
_______________________________________
/ 我们的长剑将为 \
| 我们发言。 |
| |
\ -- Christopher Marlowe /
---------------------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
期望功能内置在非阻塞命令中
.. code:: pycon
>>> c.expect('Password:')
>>> c.send('PASSWORD')
>>> c.block()
其他功能
.. code:: pycon
>>> c.kill()
>>> c.send('SIGTERM', signal=True)
# 仅当 block=True 时可用,否则请使用 c.out。
>>> c.err
''
# 直接访问管道。
>>> c.std_err
<open file '<fdopen>', mode 'rU' at 0x10a5351e0>
# 调整命令的环境变量(现有变量将被覆盖)。
>>> c = delegator.chain('env | grep NEWENV', env={'NEWENV': 'FOO_BAR'})
>>> c.out
NEWENV=FOO_BAR
安装
------------
::
$ pip install delegator.py
✨🍰✨