Skip to content

Instantly share code, notes, and snippets.

@huxuan
Last active November 28, 2019 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huxuan/d58a5899be9b42e0936c71dd7884442a to your computer and use it in GitHub Desktop.
Save huxuan/d58a5899be9b42e0936c71dd7884442a to your computer and use it in GitHub Desktop.
Subprocess failed to kill child process when using with statement.
from urllib.request import urlopen
import subprocess
import time
url = 'https://iptv-org.github.io/iptv/index.m3u'
with urlopen(url) as response:
for line in response.read().decode('utf-8').splitlines():
if line.startswith('http'):
start_time = time.time()
# with statement, does not work
with subprocess.Popen(f'ffprobe -v quiet {line}'.split(), stdout=subprocess.PIPE) as proc:
try:
outs, errs = proc.communicate(timeout=.1)
except subprocess.TimeoutExpired:
proc.kill()
# # NO with statement, work
# proc = subprocess.Popen(f'ffprobe -v quiet {line}'.split(), stdout=subprocess.PIPE)
# try:
# outs, errs = proc.communicate(timeout=.1)
# except subprocess.TimeoutExpired:
# proc.kill()
print(f'{time.time() - start_time}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment