Skip to content

Commit 72359b5

Browse files
feat: improved performance for recv calls
1 parent 4ab402e commit 72359b5

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

avisengine.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,25 @@ def connect(self,server,port):
6969
print("Failed to connect to ", server, port)
7070
return False
7171

72+
7273
def recvall(self, socket):
7374
'''
7475
Function to receive all the data chunks
7576
'''
76-
BUFFER_SIZE = 131072
77-
data = ""
77+
BUFFER_SIZE = 65536 # Increased buffer size for better performance
78+
data = bytearray() # Use a bytearray for better performance
79+
7880
while True:
79-
part = socket.recv(BUFFER_SIZE).decode("utf-8")
80-
data += part
81+
part = socket.recv(BUFFER_SIZE)
82+
data.extend(part)
83+
8184
# Use KMP search to find the <EOF>, KMPSearch() returns -1 if the pattern was not found
82-
if(utils.KMPSearch("<EOF>", data) > -1):
85+
# It is 9 times faster than the simple python search
86+
if utils.KMPSearch(b"<EOF>", data) > -1: # Convert "<EOF>" to bytes
8387
break
84-
85-
return data
88+
89+
return data.decode("utf-8")
90+
8691

8792
def setSteering(self,steering):
8893
'''

example.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# Sleep for 3 seconds to make sure that client connected to the simulator
2222
time.sleep(3)
2323

24+
2425
try:
2526
while(True):
2627
# Counting the loops
@@ -38,7 +39,9 @@
3839

3940
# Get the data. Need to call it every time getting image and sensor data
4041
car.getData()
42+
4143

44+
# Display the FPS on the frame
4245
# Start getting image and sensor data after 4 loops
4346
if(counter > 4):
4447

@@ -56,14 +59,16 @@
5659
print(f"Speed : {carSpeed}")
5760
print(f'Left : {str(sensors[0])} | Middle : {str(sensors[1])} | Right : {str(sensors[2])}')
5861

59-
# Showing the opencv type image
60-
cv2.imshow('frames', image)
62+
if image is not None and image.any():
63+
# Showing the opencv type image
64+
cv2.imshow('frames', image)
65+
6166

6267

6368
if cv2.waitKey(10) == ord('q'):
6469
break
6570

66-
time.sleep(0.001)
71+
6772

6873
finally:
6974
car.stop()

0 commit comments

Comments
 (0)