Skip to main content
Author : Yves-Gwenael Bourhis

==================================================
Wrap a shell comand into a python threaded object.
==================================================

Usage:
======

You want to launch the following bash commands in a thread::

[user@localhost ~]$ ls -l | grep pdf | wc -l
5

here is how you can do it::

>>> Ls = WrapCommand( 'ls -l')
>>> GrepPdf = WrapCommand( 'grep pdf')
>>> Wc = WrapCommand( 'wc -l')
>>> Wc.stdin = GrepPdf
>>> GrepPdf.stdin = Ls
>>> Wc.start( )
>>> #Do stuff
...
>>> Wc.join()
>>> Wc.results
('5\n', '')

the 'results' property is a tuple (stdoutdata, stderrdata)

You can also do it this way::

>>> Ls = WrapCommand( 'ls -l | grep pdf | wc -l', shell=True)
>>> Ls.start()
>>> #Do stuff
>>> Ls.join()
>>> Ls.results[0]
'5\n'

You would need to specify 'shell=True' when the command
you wish to execute is actually built into the shell.
i.e.: on Windows if you use built in commands such as 'dir' or 'copy':
http://docs.python.org/library/subprocess.html#subprocess.Popen

The purpose of doing it in a thread is when the above commands may
take a few hours, and that you want to perform other tasks in the
meanwhile.
You can check the process is still running with::

>>> Wc.is_alive( )
False

'True' would be returned if still running.
To terminate it prematurely (i.e. it deadlocked) you have the
'terminate()', 'kill()' or 'send_signal(signal) methods which are
self speaking.
When you want to wait for the thread to end, use the 'join()' method:
http://docs.python.org/library/threading.html#threading.Thread.join


You want to launch the following bash commands without threading::

[user@localhost ~]$ ls -l | grep pdf | wc -l
5

here is how you can do it::

>>> Ls = WrapCommand( 'ls -l')
>>> GrepPdf = WrapCommand( 'grep pdf')
>>> Wc = WrapCommand( 'wc -l')
>>> Wc(GrepPdf(Ls))
'5\n'

Avoid doing this for processes where a large amount of data is piped
between each command.

instead, do it this way::

>>> Ls = WrapCommand( 'ls -l | grep pdf | wc -l', shell=True)
>>> Ls()
'5\n'

Prefer the threaded method instead if this may take a long time and
that you want to perform other tasks in the meanwhile.


You can specify another shell for running commands::

>>> Ls = WrapCommand( 'ls', shell=True, executable='C:/windows/System32/WindowsPowerShell/v1.0/powershell.exe')
>>> print Ls()

Directory : C:\Users\Yves\python_tests

Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 27/01/2011 00:14 7006 commandwrapper.py
-a--- 27/01/2011 00:15 7048 commandwrapper.pyc


You can also use Context Management (with_item):
http://docs.python.org/reference/compound_stmts.html#grammar-token-with_item

example::

>>> with WrapCommand( 'ls -l') as Ls:
... with WrapCommand( 'grep pdf') as GrepPdf:
... with WrapCommand( 'wc -l') as Wc:
... Wc.stdin = GrepPdf
... GrepPdf.stdin = Ls
... Wc.start( )
... #Do stuff
... Wc.join()
...
>>> Wc.results
('5\n', '')

You may also simply want to have a subprocess objet::

>>> ls = WrapCommand( 'ls -l')
>>> lscmd = ls.makeCmd()
>>>

the returned object (`lscmd` in the example above) is a standard subprocess.Popen object


WrapOnceCommand is the same as WrapCommand, but the cmd attribute
which is a subprocess.Popen object will be created once and for all
Therefore the run methode (or the object) can only be called once.
The goal it to launch a command in a thread, and to have this
command easily start/stopped from elsewhere.

Metadata

Release files for commandwrapper 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for commandwrapper 0.2
File Size Uploaded Provenance Actions
commandwrapper-0.2.tar.gz 3.5 kB Without provenance Details for commandwrapper-0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for commandwrapper 0.2
File Interpreter ABI Platform Actions
commandwrapper-0.2.linux-x86_64.tar.gz Details for commandwrapper-0.2.linux-x86_64.tar.gz

Total release size: 9.2 kB

Release files / commandwrapper-0.2.tar.gz

Download URL commandwrapper-0.2.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
969086f303d662fd322e68f08aa821c470b77dab0c91d73ca141e8c28e5bc3af
BLAKE2b-256 checksum
How to use checksums
a0cc61eca4a91dc421dda38d393f857818eb1693c0419d0fce90663384175902
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / commandwrapper-0.2.linux-x86_64.tar.gz

Download URL commandwrapper-0.2.linux-x86_64.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
909d688f7a1e65a2194b2fd846e73431c03920979a50ba603f7536cea97c930f
BLAKE2b-256 checksum
How to use checksums
016c5ee106b8a189dcadc2fa527d639726d582c5d5c3f744d3a98f911601f702
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for commandwrapper 0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page