Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading from socket file handle took too long

Reply
Thread Tools

Reading from socket file handle took too long

 
 
Etienne Desautels
Guest
Posts: n/a
 
      02-24-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

 
Reply With Quote
 
 
 
 
Ben Sizer
Guest
Posts: n/a
 
      02-27-2006
Etienne Desautels wrote:
> 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.


If you're sure this is the problem, you might try calling setSockOpt on
the socket to set the nodelay flag, something like this:

mySocket.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)

I don't know enough about the option to be sure it'll work, but it
can't hurt to try.

--
Ben Sizer

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Having compilation error: no match for call to (const __gnu_cxx::hash<long long int>) (const long long int&) veryhotsausage C++ 1 07-04-2008 05:41 PM
Allow Roles name too long?? AD groups too long? Integrated Security wildman@noclient.net ASP .Net 0 03-10-2008 03:47 PM
Can a URL be too long for Netscape to handle? Jeff Wisnia Computer Support 12 08-25-2006 04:36 PM
how do I know how long my function call took? Kevin Schultz C Programming 4 07-19-2005 01:28 AM
File Handle Reading Blues: Rereading a File Handle for Input Dietrich Perl 1 07-22-2004 10:02 AM



Advertisments