I want to import subprocess
module from py v3.3 to v2.7 to be able to use the timeout
functionality.
After reading few posts I tried this
from __future__ import subprocess
But it says:
SyntaxError: future feature subprocess is not defined
Then I found out that future doesn't have any feature subprocess
.
So where and how should I import the subprocess
from v3.3?
I think the backport is a good idea. Here is a comparison of subprocess.call
. Note that having the named parameter timeout
after *popenargs
is a syntax error in Python2, so the backport has a workaround. The timeout parameter for the other functions is handled similarly. You should look at the wait
method of Popen
if you are interested in how the timeout is actually implemented.
def call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
return Popen(*popenargs, **kwargs).wait()
def call(*popenargs, timeout=None, **kwargs):
"""Run command with arguments. Wait for command to complete or
timeout, then return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
with Popen(*popenargs, **kwargs) as p:
try:
return p.wait(timeout=timeout)
except:
p.kill()
p.wait()
raise
def call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete or
timeout, then return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
timeout = kwargs.pop('timeout', None)
p = Popen(*popenargs, **kwargs)
try:
return p.wait(timeout=timeout)
except TimeoutExpired:
p.kill()
p.wait()
raise
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With