Reading from socket file handle took too long

Etienne Desautels tiit at sympatico.ca
Fri Feb 24 16:25:49 EST 2006


Hi,

I'm working on project where I need to grab video from an Axis IP  
camera. This camera send a stream of a multipart message on HTTP. I  
write this code (at the bottom of the message) to read and uncompress  
each jpeg images. I call nextFrame() 24 times per seconds (or every  
0,0416 sec.) to read the next image that arrived.

Everything works well but one thing. My problem is that running  
nextFrame() took, most of the time, more then 0,0414 sec. so my video  
is lagging. I monitor every call and I found that the culprit is when I  
read from the socket file handle. It's the only bottleneck in my code.  
I try different approaches to the problem but I always hit the same  
problem.

I don't think it's normal that read() take more then 0,04 sec to read  
1000 bytes from memory (the socket file handle). How can I avoid this  
problem ?

I'm on Linux 2.6 (last Ubuntu) with python 2.4 on P4 2.3.

Thanks

Etienne


CODE
------------------------------------------------------------------------ 
----------------------------

class Axis:
	url = ""
	user = "user"
	password = "password"
	header = {}
	header["Authorization"] = "Basic " + b64encode(user + ":" + password)
	http = urllib2.HTTPHandler()
	state = 0
	limit = 1000
	imageData = None
	sizeX = 352
	sizeY = 240
	fps = 24
	compression = 25
	
	def startGrab(self):
		try:
			url = self.url + "/axis-cgi/mjpg/video.cgi?resolution=" +  
str(self.sizeX) \
			+ "x" + str(self.sizeY) + "&compression=" \
			+ str(self.compression) + "&req_fps=" + str(self.fps)
			req = urllib2.Request(url, None, self.header)
			self.fh = self.http.http_open(req)
			self.buffer = ""
			self.state = 1
		except:
			self.state = 0
	
	def stopGrab(self):
		self.fh.close()
		self.state = 0
	
	def nextFrame(self):
		if self.state:
			try:
				temp = self.fh.read(self.limit)	# <-- TAKE A LOT OF TIME
				self.buffer += temp
				boundary = self.buffer.find("\r\nContent-Type: image/jpeg\r\n") + 30
				if boundary >= 30:
					end = self.buffer.find("\r\n\r\n--myboundary", boundary) + 2
					if end >= 2:
						image = Image.open(StringIO(self.buffer[boundary:end]))
						self.imageData = image.tostring("raw",image.mode,0,-1)
						self.buffer = self.buffer[end:]
									
			except:
				pass




More information about the Python-list mailing list