Shell damage after killing a subprocess

I know that there are several similar questions on SO, for example this or this and maybe a couple more, but none of them seem to be applicable in my specific situation. A lack of understanding of how it works subprocess.Popen()does not help.

What I want to achieve: start a subprocess (command line radio player), which also outputs data to the terminal and can also accept input data - wait a while - stop the subprocess - exit the shell. I am running python 2.7 on OSX 10.9

Case 1. This starts the radio player (but only audio!), Terminates the process, exits.

import subprocess
import time

p = subprocess.Popen(['/bin/bash', '-c', 'mplayer http://173.239.76.147:8090'],
                     stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=False,
                     stderr=subprocess.STDOUT)
time.sleep(5)
p.kill()

Case 2. This starts the radio player, displays information such as the name of the radio, song, bit rate, etc., and also receives input data. It completes the subprocess, but the shell never exists, and the terminal becomes unusable even after using "Ctrl-C".

p = subprocess.Popen(['/bin/bash', '-c', 'mplayer http://173.239.76.147:8090'],
                     shell=False)
time.sleep(5)
p.kill()

Any ideas on how to do this? I even thought about the possibility of opening a slave shell for the subprocess, if there is no other choice (of course, this is also something that I do not know about). Thank!

+4
source share
2 answers

It seems to be mplayerusing the library curses, and when kill()ing or terminate()ing, for some reason, it does not clear the state of the library correctly.

, reset.

:

import subprocess, time

p = subprocess.Popen(['mplayer', 'http://173.239.76.147:8090'])
time.sleep(5)
p.terminate()
p.wait()  # important!

subprocess.Popen(['reset']).wait()

print('Hello, World!')

, stty sane, .


, () wait(). wait() terminate() ( reset).

wait() python mplayer.

, , mplayer, , , q stdin mplayer, .

, reset, , curses, , , , , isn .

+3

: ( ), - - - . python 2.7 OSX 10.9

mplayer , , q, :

#!/usr/bin/env python
import shlex
import time
from subprocess import Popen, PIPE

cmd = shlex.split("mplayer http://www.swissradio.ch/streams/6034.m3u")
p = Popen(cmd, stdin=PIPE)
time.sleep(5)
p.communicate(b'q')

mplayer ; 5 ; mplayer . ( , python script).

p.kill(), p.terminate(), p.send_signal(signal.SIGINT) (Ctrl + C). p.kill() , . : p.kill() , , stdout=PIPE, Python script p.stdout.read(), mplayer , , , p.terminate(), p.send_signal(signal.SIGINT) - mplayer . , , reset.


Python, ? PIPE?

stdin=PIPE p.terminate(); p.wait() p.communicate(b'q').

stdin=PIPE, : sys.stdin, p.stdin , -. , mplayer , sys.stdin. : p.stdin.write(c) ( bufsize=0, Python. mplayer stdin, ).

. timeout, threading.Timer(5, p.stdin.write, [b'q']).start() select.select sys.stdin .

, -, raw_input, , ?

raw_input() mplayer, , mplayer .

+2

Source: https://habr.com/ru/post/1535278/


All Articles