Python Forum
Popen - How can I read data before CTRL+C command is issued
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Popen - How can I read data before CTRL+C command is issued
#11
(Jun-26-2018, 01:17 PM)volcano63 Wrote: Cannot be done, you say Naughty ? This was done on Ubuntu
Output:
In [23]: ping_proc = subprocess.Popen(['ping', 'google.com'], ...: stdout=subprocess.PIPE, stdin=subprocess.PIPE, ...: stderr=subprocess.STDOUT, universal_newlines=True) In [24]: for line_no, line in enumerate(ping_proc.stdout): ...: print(line,) ...: if line_no == 10: ...: print('Killing process') ...: ping_proc.kill() ...: break ...: 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=3 ttl=50 time=66.0 ms 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=4 ttl=50 time=66.1 ms 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=5 ttl=50 time=66.3 ms 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=6 ttl=50 time=66.0 ms 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=7 ttl=50 time=66.2 ms 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=8 ttl=50 time=66.1 ms 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=9 ttl=50 time=66.1 ms 64 bytes from lhr25s10-in-f14.1e100.net (216.58.198.174): icmp_seq=10 ttl=50 time=66.2 ms Killing process
I redirect stderr to stdout - some processes use stderr instead of stdout, so suppressing the former is a bad idea

I tried the above code.

With ping it worked as expected and with the tool I am using I got nothing until I pressed CTRL+C again.

any ideas are appreciated as I am pretty much out!!
Reply
#12
(Jun-27-2018, 03:22 PM)maffaz Wrote: With ping it worked as expected and with the tool I am using I got nothing until I pressed CTRL+C again.

any ideas are appreciated as I am pretty much out!!

Maybe, try send_signal with CTRL_C_EVENT. I cannot suggest anything more - very strange behavior of the app.
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#13
(Jun-27-2018, 07:39 PM)volcano63 Wrote:
(Jun-27-2018, 03:22 PM)maffaz Wrote: With ping it worked as expected and with the tool I am using I got nothing until I pressed CTRL+C again.

any ideas are appreciated as I am pretty much out!!

Maybe, try send_signal with CTRL_C_EVENT. I cannot suggest anything more - very strange behavior of the app.

When I say CTRL+C i meant kill(self.proc.pid, signal.CTRL_C_EVENT) :).

Yes I have tried everything.. I am programming it into a PyQt App and I have also gone through all of their Qprocess commands as well.

It is nice to have a second opinion so thank you very much.

I think I will have to go to the C developer and ask him to recode it to send it via STDOUT or ERR or anywhere!!

I will hopefully have an answer once I can catch him and I will post here to satisfy anyone's curiosities!
Reply
#14
for anyone that is interested. I have found the problem but it is not Python Related, Rather the C implementation of the tool..
Quote:The problem here is probably, that the output of your CLI program is block buffered, since the output file descriptor is not associated with a terminal. You claim, that it works with "ping" instead of your cli program, so it definitively has to be an issue with the subprocess itself.

Since you programmed the cli program yourself, make sure that it does not buffer the output (i.e. put an fflush() after each line which is printed.

You can read the discussion here..

https://stackoverflow.com/questions/5103...1#51078297
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 317 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Correctly read a malformed CSV file data klllmmm 2 1,813 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  Read nested data from JSON - Getting an error marlonbown 5 1,309 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Read data via bluetooth frohr 9 3,146 Jul-10-2022, 09:51 AM
Last Post: frohr
  Write and read back data Aggie64 6 1,811 Apr-18-2022, 03:23 PM
Last Post: bowlofred
  How to read rainfall time series and insert missing data points MadsM 4 2,123 Jan-06-2022, 10:39 AM
Last Post: amdi40
  Python continues after CTRL-C kjbolhuis 2 1,850 Aug-06-2021, 04:28 PM
Last Post: kjbolhuis
  [Solved] Using readlines to read data file and sum columns Laplace12 4 3,464 Jun-16-2021, 12:46 PM
Last Post: Laplace12
  Looping to read data in database CEC68 1 1,677 Sep-24-2020, 08:54 PM
Last Post: scidam
  Assigning data read from CSV to class faruk61 2 2,075 Apr-15-2020, 05:52 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020