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 8104399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:49:42+00:00 2026-06-05T23:49:42+00:00

Let’s say you had a fairly basic client/server code, where each client creates three

  • 0

Let’s say you had a fairly basic client/server code, where each client creates three threads and multiple clients can connect at once. I want the server to wait for an incoming connection, and once it starts getting connections, to run until there are no more threads running, then exit. Code is similar to below. (ie, rather than the server “serve forever,” I want it to exit once all the threads finish).

edit: I want the server to wait for incoming connections. Once the connections begin, it should keep accepting connections until there are no threads left running, then exit. These connections will be somewhat sporadic.

import socket
import threading

# Our thread class:
class ClientThread ( threading.Thread ):

   # Override Thread's __init__ method to accept the parameters needed:
   def __init__ ( self, channel, details ):

      self.channel = channel
      self.details = details
      threading.Thread.__init__ ( self )

   def run ( self ):

      print 'Received connection:', self.details [ 0 ]
      self.channel.send ( 'hello from server' )
      for x in xrange ( 10 ):
         print self.channel.recv ( 1024 )
      self.channel.close()
      print 'Closed connection:', self.details [ 0 ]

# Set up the server:
server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
server.bind ( ( '', 2727 ) )
server.listen ( 5 )

# Have the server serve "forever":
while True:
   channel, details = server.accept()
   ClientThread ( channel, details ).start()
  • 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-06-05T23:49:43+00:00Added an answer on June 5, 2026 at 11:49 pm

    As per your comments, what you are looking for is to starting counting connections after the first one is made to the server, and to kill the server once there are no more existing connections.

    An immediate problem with your current infinite while loop is that is blocks on each accept(). So no matter what, it will always be waiting for another connection. You would have to interrupt it from some other thread to get it out of that loop. But another solution would be to make your event loop larger, and the act of accepting a new connection is only one part of it. The loop should also be checking for a condition to exit.

    This example is only one possible way. It makes use of a Queue.Queue to coordinate the work counter.

    import socket
    import threading
    import select
    from Queue import Queue
    
    class ClientThread ( threading.Thread ):
    
       def __init__ ( self, channel, details, queue=None ):
          self.channel = channel
          self.details = details
          self.queue = queue
          threading.Thread.__init__ ( self )
    
       def run ( self ):
    
          if self.queue:
             self.queue.put(1)
    
          print 'Received connection:', self.details [ 0 ]
          self.channel.send ( 'hello from server' )
          for x in xrange ( 10 ):
             print self.channel.recv ( 1024 )
          self.channel.close()
          print 'Closed connection:', self.details [ 0 ]
    
          if self.queue:
             self.queue.get_nowait()
    
    # Set up the server:
    server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
    server.bind ( ( '', 2727 ) )
    server.listen ( 5 )
    
    rlist = [server]
    work_queue = Queue()
    
    def accept_client():
       channel, details = server.accept()
       ClientThread ( channel, details, work_queue ).start() 
    
    accept_client()
    
    while not work_queue.empty():
       server_ready, _, _ = select.select(rlist,[],[], .25)
       if server in server_ready:
          accept_client()
    
    print "Shutting down"
    server.close()
    print "Exiting"
    

    We use select.select as a simple way to detect activity on your server socket, but also with a timeout. If the server is ready, then we accept the new connection. If it reaches the .25 second timeout, we just loop again and wait.

    You will see that we create a queue and constantly check if its empty. The queue is passed into each thread. When a thread is started, it logs some work into the queue. The data is arbitrary. Just a flag. When the thread has completed, it will clear that item from the queue. The result is that after the first connection is received, the queue is no longer empty, and the loop will keep running. If at any point the queue becomes empty (because all current threads have finished), the loop will break and the server will shut down.

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

Sidebar

Related Questions

Let's say after I had login I will be prompt to enter the Name
Let's say I have this code: <p dataname=description> Hello this is a description. <a
Let's say I have multiple requirements for a password. The first is that the
Let say I have some code HTML code: <ul> <li> <h1>Title 1</h1> <p>Text 1</p>
Let's say we have this code: <form action='' method='POST' enctype='multipart/form-data'> <input type='file' name='userFile'><br> <input
Let me try to explain by example. Say website is hosted at example.com (NOT
Let's say I have a table with a Color column. Color can have various
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis

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.