Python Subprocess readline hangs() after reading all input

2024/10/16 3:27:36

I am trying to readlines from the tcp server that I ran in the same script. I am able to send one command and reads its output but at the end (after reading all outputs) it looks like that program hangs in readline, I have tried almost all the solutions here and here but still it hangs.

Most of these solutions propose to check if output of readline is none or not but in my case program never returns from last read and just hangs in there.

tcp server is not in my control, or say I just have to test server script therefore I can not modify it. Also, is it possible to send commands to runing server using python without using subprocess? any better alternative?

def subprocess_cmd(command):process=subprocess.Popen(command,stdin=subprocess.PIPE,stderr=subprocess.STDOUT,stdout=subprocess.PIPE, shell=True)for cmd in ['python3 -u tcp_server.py 123 port1']:subprocess_cmd(cmd)process.stdin.write('command like print_list')
process.stdin.flush()
while True:line=process.stdout.readline()if line == '':break
Answer

readline hangs because your TCP connection is still open and readline expects more data to come in. You must close the connection from server side to notify readline that there is nothing more to read. Usually it is done by closing socket on client side notifying the server that there will not be any more requests to it. When server finishes processing all your commands it closes socket too. And this is the signal for you that you have received all the data that server sent to you.

Or, alternatively, if you don't want to close the connection, you must invent delimiters which will mark end of response. So the client will stop calling readline when such delimiter is read.

https://en.xdnf.cn/q/117760.html

Related Q&A

Python Counting countries in dictionary

Im writing a function that counts the number of times a country appears in a dictionary and returns the country that appeared the most. If more then one country appears the most then it should return a…

How to wait for any socket to have data?

Im implementing a socket-client which opens several sockets at the same time. Any socket may have data at a different time and I want to execute code when any socket has data and is readable.Im not sur…

Retrieving ad URLs

Im looking for a way to retrieve the ad URLs for this website. http://www.quiltingboard.com/resources/What I want to do is probably write a script to continuously refresh the page and grab the ad URLs.…

Extract data (likes) from JSON API using Python

I want to pull the number of likes for my project. Heres my code: import facepy from facepy import GraphAPI from bs4 import BeautifulSoup import json access = CAACEdEose0cBAE3IL99IreDeAfqaVZBOje8ZCqIhf…

No nested nodes. How to get one piece of information and then to get additional info respectively?

For the code below I need to get dates and their times+hrefs+formats+...(not shown) respectively.<div class="showtimes"><h2>The Little Prince</h2><div class="poster&…

I need help changing the color of text in python

Hey I need help with coloring the text in a program I am making. It is a password program and I am trying to make the denied and granted red and green when they appear. Here is the program so far:passw…

How do I use Liclipse to write a ParaView script?

Ive tried following the directions here without success. Here are some of my environment variables:Path: C:\Python34\;C:\Python34\Scripts;...;C:\Program Files (x86)\ParaView 4.3.1\lib\paraview-4.3\site…

List of tuples to nested dictionary without overriding

I need to convert the above list of tuples to nested dictionary without overwriting the value as below in python[(a, 1),(b, true),(b, none),(a, 2),(b, true),(a, 3),(b, false)]{a: {1 : { b : (true,none)…

Rotate matplotlib pyplot with curve by 90 degrees

I have plot with one line as this:import numpy as np import matplotlib.pyplot as pla = np.array([4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9]) b = np.array([i/len(a) for i in range(1,…

Reading csv file and returning as dictionary

Ive written a function that currently reads a file correctly but there are a couple of problems. It needs to be returned as a dictionary where the keys are artist names and the values are lists of tupl…