Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Warrick Newsgroups: comp.lang.python Subject: Re: Using subprocess to capture a progress line Date: Wed, 11 Nov 2015 10:48:03 +0100 Lines: 59 Message-ID: References: <20151110224756.GA1944@mail.akwebsoft.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de aeePvnDCR4FDrjFQVLWczAZ1+DZEePj54PF8EoA+sB8Q== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'messages.': 0.04; 'bash': 0.07; 'finished.': 0.07; 'line:': 0.07; 'tmp': 0.07; 'mess': 0.09; '\xe2\x80\x94': 0.09; 'python': 0.10; 'output': 0.13; 'carriage': 0.16; 'naq': 0.16; 'overwriting': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'repr()': 0.16; 'true:': 0.16; 'worst': 0.16; 'wrote:': 0.16; 'library,': 0.18; 'runs': 0.18; '2015': 0.20; 'this:': 0.23; 'implemented': 0.24; 'tim': 0.24; 'written': 0.24; 'header:In-Reply-To:1': 0.24; 'header': 0.24; "i've": 0.25; 'command': 0.26; 'chris': 0.26; 'figure': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; 'command- line': 0.29; 'reply-to': 0.29; 'sensible': 0.29; 'character': 0.29; 'read,': 0.29; 'print': 0.30; 'code': 0.30; 'skip:[ 10': 0.31; 'run': 0.33; 'downloading': 0.33; 'ubuntu': 0.33; 'file': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'i.e.': 0.35; 'something': 0.35; 'problem.': 0.35; 'should': 0.36; 'there': 0.36; 'received:209.85': 0.36; '(3)': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'setting': 0.37; '(2)': 0.37; 'johnson': 0.37; 'things': 0.38; 'progress': 0.38; 'received:209': 0.38; '(1)': 0.38; 'reports': 0.38; 'thank': 0.38; 'end': 0.39; 'means': 0.39; 'does': 0.39; 'application': 0.39; 'skip:- 60': 0.39; 'to:addr:python.org': 0.40; 'your': 0.60; 'show': 0.62; 'times': 0.63; 'numerous': 0.66; 'fact,': 0.67; '8bit%:21': 0.70; '100%': 0.72; 'i\xe2\x80\x99ll': 0.84; 'subject:Using': 0.84; 'ps.': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=7dkJ+KTSfOOp7SAHJM3gH1a6zIwJAfLpyHQh9q89I7Y=; b=SrnYkWLgjoRVCJAToHCpA+hUQyDQkKErtvGaytCvJ5L/++qE/s14gCLhe0guOho6wE T1e+9ky7CDkbPfFresIPzT81jtAp1VzVKvdey+EJJZ66I7KgzI0rqtazK8i/WoZGk/Zv jOdggNAYGLu1ZvS1OY7ghbBxjn4rp+P+SDEydwTkLP5tWE6tK0nqJDeYY739HmdNR8Y1 4XOG4DALzUu4mSAbuvMaLUtbF7/NtCKR/xv+46cfcNn2b/qwbmLBP+R2nlD+AgZkUf33 Xpynk5MF1jKYeibwSiKrn+OCJw/8yt7MjE4SNyxiHUKCQK1EI/RPNw6fHOWZR5YOrS6e oZow== X-Received: by 10.13.201.68 with SMTP id l65mr8145639ywd.340.1447235283947; Wed, 11 Nov 2015 01:48:03 -0800 (PST) In-Reply-To: <20151110224756.GA1944@mail.akwebsoft.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:98634 On 10 November 2015 at 23:47, Tim Johnson wrote: > Using python 2.7.6 on ubuntu 14.04 > The application in question is run with bash and gnome-terminal : > > I've written a command-line "wrapper" for youtube-dl, executing > youtube-dl as a subprocess. > > ------------------------------------------------------------------ > youtube-dl reports download progress on one line. I.E. the line is > overwritten numerous times with no carriage return until the > downloading is finished. > ------------------------------------------------------------------ > > The following code runs the youtube-dl command and reports each line > as output by youtube-dl > ########### > p =3D subprocess.Popen(list(args), stderr=3Dsubprocess.STDOUT, > stdout=3Dsubprocess.PIPE) > while True: > line =3D p.stdout.readline() > if not line: > break > tmp =3D line.strip() > print tmp > ########### > > However this method not does show the download progress _until_ the > download is complete. There is no \n character at the end =E2=80=94 which means that p.stdout.readline() cannot return. In fact, if you printed repr() of the line you read, you would get this: b'\r[download] 54.9% of 2.73MiB at 26.73KiB/s ETA 00:47\r[download] 55.0% of 2.73MiB at 79.33KiB/s ETA 00:15\r=E2=80=A6snip=E2=80=A6\r[download= ] 100% of 2.73MiB in 00:01\n' The download line is implemented using \r, which is the carriage return character (return to the first character), and then by overwriting characters that were already printed. The solution? There are numerous. I=E2=80=99ll help you by obscuring the wo= rst one. (1) [recommended] figure out how to make youtube_dl work as a library, read its main file to figure out the problem. Don=E2=80=99t mess with subprocess. (2) [don=E2=80=99t do it] do you need to intercept the lines? If you don=E2= =80=99t set stderr=3D and stdout=3D, things will print just fine. (3) [DON=E2=80=99T DO IT] .ernq() punenpgre ol punenpgre naq znxr n zrff. PS. Thank you for setting a sensible Reply-To header on your messages. Which is something the list should be doing. --=20 Chris Warrick PGP: 5EAAEA16