Reading from socket file handle took too long

Discussion in 'Python' started by Etienne Desautels, Feb 24, 2006.

  1. 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
     
    Etienne Desautels, Feb 24, 2006
    #1
    1. Advertising

  2. Etienne Desautels

    Ben Sizer Guest

    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
     
    Ben Sizer, Feb 27, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    650
    Joe Smith
    Jul 22, 2004
  2. vincent wehren
    Replies:
    6
    Views:
    410
  3. Kevin Schultz

    how do I know how long my function call took?

    Kevin Schultz, Jul 18, 2005, in forum: C Programming
    Replies:
    4
    Views:
    392
  4. Replies:
    0
    Views:
    593
  5. Daniel Pitts
    Replies:
    1
    Views:
    334
    Tom Anderson
    Apr 9, 2010
Loading...

Share This Page