'How and when to process the data received by socket client in python?
I have created a socket client in python that is meant to receive data from socket server (which is sending data to client at constant time interval) and construct an image out of the data by using cv2 and numpy. I have tested the Socket Client and Server for echo communication for text data and it is working as expected. The Socket Server is ESP32CAM Access Point and it is also working as expected. I am facing issue when I try to write the Socket Client python code, because as the ESP32CAM Socket Server is sending data at constant time interval, the Socket Client has to be ready to receive data at that exact moment, which is the issue I have. I would like to know whether there is some concept like Interrupts in python like microcontrollers.
Here is my python code :
import socket
import sys
import time
import io
import cv2
import numpy
import numpy as np
host = "192.168.4.1"
port = 80
print('# Creating socket')
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print('Failed to create socket')
sys.exit()
print('# Getting remote IP address')
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
print('Hostname could not be resolved. Exiting')
sys.exit()
print('# Connecting to server, ' + host + ' (' + remote_ip + ')')
sock.connect((remote_ip , port))
print('Server Connected !')
while True:
begin = time.time()
message_chunks = []
d = ''
while True:
if time.time() - begin > 0.2: #200 ms
break
try:
d = sock.recv(4194304) #4MB data
#print(d)
except socket.timeout:
break
if d:
message_chunks.append(d)
else:
break
data = b''.join(message_chunks)
img = io.BytesIO()
img.write(data)
img.seek(0)
imgnp = numpy.array(bytearray(img.read()), dtype=np.uint8)
frame = cv2.imdecode(imgnp,-1)
cv2.startWindowThread()
cv2.namedWindow("stream", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("stream",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
cv2.imshow("stream", frame)
I kindly request for some guidance in threading and parallel processing for this code as well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
