Results 1 to 2 of 2

Thread: Shoul i keep a socket open for long time?

  1. #1
    Join Date
    Jun 2010
    Location
    Loznica Serbia
    Beans
    123
    Distro
    Ubuntu 12.04 Precise Pangolin

    Shoul i keep a socket open for long time?

    I have two computers on a network and some python programs running on them. Occasionally the programs will send some data to each other. There may be a long silence between individual messages. Should I keep an open socket all the time or create a new socket for every message?
    Which of these is better:
    This one
    Code:
    #keeps an open socket
    s=socket(...)
    s.connect(someAddress)
    while True:
        s.send(someData)
        sleep(five minutes)
    or this one
    Code:
    #creates a new socket for each individual communication
    while True:
        s=socket(...)
        s.connect(someAddress)
        s.send(someData)
        s.close()
        sleep(five minutes)
    Windows is not user friendly,it's just user familiar

  2. #2
    Join Date
    Nov 2009
    Beans
    17

    Re: Shoul i keep a socket open for long time?

    My read of this page: http://docs.python.org/2/howto/sockets.html makes it sound like you're ever-so-slightly better off closing the socket when you're done sending.

    Makes some intuitive sense to me... if the sending computer shuts off or gets disconnected by your cat/kid/housecleaner (or, more likely, crashes/hangs on some other line of code you've written and for some reason doesn't get to the normal garbage collection), the listening end could hang on their socket f it's still open. I'd doubt it'll matter too much beyond that, assuming you get the right behavior with both styles that you mentioned (ie, that there's not a noticeable overhead to opening the socket in the first place).

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •