Reputation: 171
I have written small programs to create both client and server on the host machine using python sockets. On the other side of the connection, I have my FPGA board which runs LwIP client and server also, and the connection is Gbit ethernet. My aim is to send and receive dummy data and monitor the throughput of both client and server.
client.py
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_SNDBUF
send_data = []
for i in range(1458):
send_data.append(i % 256)
send_data = bytearray(send_data)
CLIENT_SOCK = socket(AF_INET, SOCK_STREAM)
CLIENT_SOCK.setsockopt(SOL_SOCKET, SO_SNDBUF, 524288)
CLIENT_SOCK.connect(("192.168.1.10", 42000))
print("CLIENT CONNECTED")
i=0
while True:
#print(i)
#i+=1
CLIENT_SOCK.sendall(send_data)
server.py
from socket import socket, AF_INET, SOCK_STREAM, SOL_SOCKET, SO_RCVBUF
SERV_SOCK = socket(AF_INET, SOCK_STREAM)
SERV_SOCK.setsockopt(SOL_SOCKET, SO_RCVBUF, 524288)
SERV_SOCK.bind(("192.168.1.1", 42001))
SERV_SOCK.listen(1)
conn, b = SERV_SOCK.accept()
print("CLIENT ACCEPTED")
i=0
while True:
#print(i)
#i+=1
data = conn.recv(16384)
I run these codes in separate command lines. When I run only one of them, I get ~800 Mbit/s throughput for server.py, and ~950 Mbit/s for client.py. However, the problem arises when I run them at the same time. At this time, I still get the same speed for server.py but I only get a fluctuating throughput between ~100 Kbit/s and ~10 Mbit/s throughput for client.py.
When I uncomment the print statements in the while loops, I saw that in server.py the print statements are printing on the command line smoothly and very fast, but on the other hand client.py prints some and then stops for a while and then prints again but not in a fast manner. I tried to reduce the bandwidth of the server.py by decreasing the size of the receive bytes from 16384 to 256, and I saw that when I run it individually the throughput is around ~500 Mbit/s. However, again, when I run them at the same time, client.py throughput showed a similar pattern as previous. I tried to increase/decrease the send_data size, or adding "\r\n" at the end of the send_data but it didn't work.
Does anyone have any idea what might be the problem and the possible solution?
Thanks.
Upvotes: 0
Views: 37
Reputation: 123290
python socket client slows down when server is also running
In TCP a client will only send data as fast as the server can receive and ACK these (and vice versa). This means another perspective of looking at your problem is not a slow Python client but a slow LwIP server.
Since you run both LwIP client and server on the FPGA there might be a bad management of resources between these. If there is only one LwIP server/client running (and thus only one Python client/server), it will get all the resources on the FPGA and thus can run with max speed. If instead both Python client/server are running there will also be both LwIP both server/client running on the FPGA - and the system resources must be properly managed between these.
The results you get suggest that your LwIP client on the FPGA is getting most of the resources though, which means that the Python server will get max bandwidth. The LwIP server on the other hand will get far less resources which makes it slow - which thus causes the Python client to be slow.
Thus, don't look on the Python side but instead look on your resource managed between LwIP client and server on the FPGA.
Upvotes: 1