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

  • SEARCH
  • Home
  • 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 8393499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:48:23+00:00 2026-06-09T19:48:23+00:00

I am developing a client-server application where whenever a new client connects to the

  • 0

I am developing a client-server application where whenever a new client connects to the server, the server spawns a new process using the multiprocessing module. Its target function is a function where it takes the socket and does I/O. The problem I have is once the TCP connection is closed between the client and the process on the server how/where do I put the .join() function call to end the child process? Also do I need to do any waitpid in the parent process like in C?

Server code:

def new_client(conn_socket):
    while True:
        message = conn_socket.recv(BUFFER_SIZE)
        conn_socket.send(message)  
        #just echo the message
        #how to check to see if the TCP connection is still alive?
        #put the .join() here??



def main():
    #create the socket
    server_socket = socket(AF_INET,SOCK_STREAM)

    #bind the socket to the local ip address on a specific port and listen
    server_port = 12000                               
    server_socket.bind(('',server_port))
    server_socket.listen(1)

    #enter in a loop to accept client connections
    while True:
        connection_socket, client_address = server_socket.accept()       
        #create a new process with the new connection_socket
        new_process = Process(target = new_client, args = (connection_socket,))
        new_process.start()
        #put the .join() here or what??

if __name__ == '__main__':
    main()

Also for this setup would it be more beneficial to use threads in the thread module or stay with processes? The server code is being developed for heavy usage on a server with “average” specs(how to optimize this setup).

  • 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-09T19:48:24+00:00Added an answer on June 9, 2026 at 7:48 pm

    You need to check the return value of recv. If it returns zero then the connection is closed nicely, if negative then there was an error.

    And the join call should be in the process that creates the sub-process. However, be carefull because join without argument will block the calling process until the sub-process is done. Put the processes in a list, and on regular intervals call join with a small timeout.

    Edit: Simplest is to add, at the end of the infinite accept loop, to iterate over the list of processes, and check if it’s is_alive. If not then call join and remove it from the list.

    Something like:

    all_processes = []
    while True:
        connection_socket, client_address = server_socket.accept()       
        #create a new process with the new connection_socket
        new_process = Process(target = new_client, args = (connection_socket,))
        new_process.start()
    
        # Add process to our list
        all_processes.append(new_process)
    
        # Join all dead processes
        for proc in all_processes:
            if not proc.is_alive():
                proc.join()
        # And remove them from the list
        all_processes = [proc for proc in all_processes if proc.is_alive()]
    

    Note that purging of old processes will only happen if we get a new connection. This can take some time, depending on if you get new connections often or not. You could make the listening socket non-blocking and use e.g. select with a timeout to know if there are new connections or not, and the purging will happen at more regular intervals even if there are no new connections.

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

Sidebar

Related Questions

I'm developing client-server application by using .Net, C#, WCF, WPF. And now I need
I am developing a client-server application using .Net Remoting. From my server I want
I am developing a Client-Server application using C# .NET Winforms with SQL Server 2008.
We are developing a server-client application in c# using wcf services and wpf. We
i am developing client server application in windows using c++ and winsock lib it
I am developing a client-server application for a cross-database system. I am using Eclipse
Im looking into developing a new version of a client/server application for a client.
I'm developing a small remote task manager application [server/client] on LAN using WCF service
I am developing a client-server-based web application using a canvas component to display data
I'm developing a client-server application and have been tasked with adding support for running

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.