Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 365847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:34:35+00:00 2026-05-12T13:34:35+00:00

I create many short-term sockets in some code that look like that : nb=1000

  • 0

I create many “short-term” sockets in some code that look like that :

nb=1000
for i in range(nb):
    sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sck.connect((adr, prt)
    sck.send('question %i'%i)
    sck.shutdown(SHUT_WR)
    answer=sck.recv(4096)
    print 'answer %i : %s' % (%i, answer)
    sck.close()

This works fine, as long as nb is “small” enough.

As nb might be quite large though, I’d like to do something like this

sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sck.connect((adr, prt)
for i in range(nb):
    reopen(sck) # ? ? ?
    sck.send('question %i'%i)
    sck.shutdown(SHUT_WR)
    answer=sck.recv(4096)
    print 'answer %i : %s' % (%i, answer)
sck.close()

So the question is :
Is there any way to “reuse” a socket that has been shutdown ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T13:34:35+00:00Added an answer on May 12, 2026 at 1:34 pm

    No, this is a limitation of the underlying C sockets (and the TCP/IP protocol, for that matter). My question to you is: why are you shutting them down when you can architect your application to use them?

    The problem with many short-term sockets is that shutting them down puts them in a state where they cannot be used for a while (basically, twice the packet lifetime, to ensure any packets in the network either arrive and are discarded, or get discarded by the network itself). Basically what happens is that, in the 4-tuple that needs to be unique (source ip, source port, destination ip, destination port), the first one and last two tend to always be the same so, when you run out of source ports, you’re hosed.

    We’ve struck this problem in software before where it only became evident when we ran on faster machines (since we could use many more sessions).

    Why dont you just open up the socket and continue to use it? It looks like your protocol is a simple request/response one, which should be easily do-able with that approach.

    Something like:

    sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sck.connect((adr, prt)
    for i in range(nb):
        sck.send('question %i'%i)
        answer=sck.recv(4096)
        print 'answer %i : %s' % (%i, answer)
    sck.close()
    

    Update:

    One possibility (and we’ve done this before) if you’re running out of connection due to this continual open/close, is to detect the problem and throttle it. Consider the following code (the stuff I’ve added is more pseudo-code than Python since I haven’t touched Python for quite a while):

    for i in range(nb):
        sck = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sck.connect((adr, prt)
    
        while sck.error() == NO_SOCKETS_AVAIL:
            sleep 250 milliseconds
            sck.connect((adr, prt)
    
        sck.send('question %i'%i)
        sck.shutdown(SHUT_WR)
        answer=sck.recv(4096)
        print 'answer %i : %s' % (%i, answer)
        sck.close()
    

    Basically, it lets you run at full speed while there are plenty of resources but slows down when you strike your problem area. This is actually what we did to our product to “fix” the problem of failing when resources got low. We would have re-architected it except for the fact it was a legacy product approaching end of life and we were basically in the fix-at-minimal-cost mode for service.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 228k
  • Answers 228k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See Calling Synchronous Methods Asynchronously The BeginInvoke will return an… May 13, 2026 at 1:38 am
  • Editorial Team
    Editorial Team added an answer That are parameterized types. You can use any identifier here… May 13, 2026 at 1:38 am
  • Editorial Team
    Editorial Team added an answer You can use the mkdir task: <mkdir dir="one/two/three" /> May 13, 2026 at 1:38 am

Related Questions

I'm writing a simple CMS based on Django. Most content management systems rely on
Many hex editors, such as Hex Workshop, can even read large-sized files while keeping
I have create a wrapper for the strncpy function. I will have to use
I have a prototype website written in PHP. Lately i've rewritten code to separate

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.