python-subprocess32
python-subprocess32 copied to clipboard
Timeout doesn't work properly
Timeout parameter doesn't really force timeout. It's happening on both Python 3.5 and Python 2.7 + subprocess32. However it works in Python 3.9.5. Not sure if it's worth to fix it, but at least it will be documented here. :)
Tests:
import os
import sys
import unittest
from datetime import datetime, timedelta
if os.name == 'posix' and sys.version_info[0] < 3:
import subprocess32 as subprocess
else:
import subprocess
class TestSubprocess(unittest.TestCase):
def test_long_sleep(self):
start = datetime.now()
try:
subprocess.run(
"sleep 10",
timeout=1,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except Exception as exc:
print("We got error:", type(exc))
delta = datetime.now() - start
print("It took", delta.seconds, "seconds")
self.assertLess(delta, timedelta(seconds=2))
if __name__ == '__main__':
unittest.main()
Results
Python 3.5.10:
We got error: <class 'subprocess.TimeoutExpired'>
It took 10 seconds
F
======================================================================
FAIL: test_long_sleep (__main__.TestSubprocess)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_subprocess.py", line 30, in test_long_sleep
self.assertLess(delta, timedelta(seconds=2))
AssertionError: datetime.timedelta(0, 10, 2203) not less than datetime.timedelta(0, 2)
----------------------------------------------------------------------
Ran 1 test in 10.003s
FAILED (failures=1)
Python 2.7.18
('We got error:', <class 'subprocess32.TimeoutExpired'>)
('It took', 10, 'seconds')
F
======================================================================
FAIL: test_long_sleep (__main__.TestSubprocess)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_subprocess.py", line 30, in test_long_sleep
self.assertLess(delta, timedelta(seconds=2))
AssertionError: datetime.timedelta(0, 10, 2108) not less than datetime.timedelta(0, 2)
----------------------------------------------------------------------
Ran 1 test in 10.003s
FAILED (failures=1)
Python 3.9.5
We got error: <class 'subprocess.TimeoutExpired'>
It took 1 seconds
.
----------------------------------------------------------------------
Ran 1 test in 1.002s
OK
Here it's described: https://bugs.python.org/issue37424 It's fixed in Python 3.7.5: https://docs.python.org/release/3.7.5/whatsnew/changelog.html#changelog