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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:54:42+00:00 2026-05-11T05:54:42+00:00

Here is source code for multithreaed server and client in python. In the code

  • 0

Here is source code for multithreaed server and client in python.

In the code client and server closes connection after the job is finished. I want to keep the connections alive and send more data over the same connections to avoid overhead of closing and opening sockets every time.

Following code is from : http://www.devshed.com/c/a/Python/Basic-Threading-in-Python/1/

import pickle import socket import threading  # We'll pickle a list of numbers: someList = [ 1, 2, 7, 9, 0 ] pickledList = pickle.dumps ( someList )  # 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 ( pickledList )       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() 

import pickle import socket import threading  # Here's our thread: class ConnectionThread ( threading.Thread ):     def run ( self ):        # Connect to the server:       client = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )       client.connect ( ( 'localhost', 2727 ) )        # Retrieve and unpickle the list object:       print pickle.loads ( client.recv ( 1024 ) )        # Send some messages:       for x in xrange ( 10 ):          client.send ( 'Hey. ' + str ( x ) + '\n' )        # Close the connection       client.close()  # Let's spawn a few threads: for x in xrange ( 5 ):    ConnectionThread().start() 
  • 1 1 Answer
  • 2 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. 2026-05-11T05:54:42+00:00Added an answer on May 11, 2026 at 5:54 am

    Spawning a new thread for every connection is a really bad design choice. What happens if you get hit by a lot of connections?

    In fact, using threads to wait for network IO is not worth it. Your program gets really complex and you get absolutely no benefit since waiting for network in threads won’t make you wait faster. You only lose by using threads in this case.

    The following text is from python documentation:

    There are only two ways to have a program on a single processor do “more than one thing at a time.” Multi-threaded programming is the simplest and most popular way to do it, but there is another very different technique, that lets you have nearly all the advantages of multi-threading, without actually using multiple threads. It’s really only practical if your program is largely I/O bound. If your program is processor bound, then pre-emptive scheduled threads are probably what you really need. Network servers are rarely processor bound, however.

    And if it is a processor bound server case. you could always leave another process/thread to do the processor part. Continuing:

    If your operating system supports the select system call in its I/O library (and nearly all do), then you can use it to juggle multiple communication channels at once; doing other work while your I/O is taking place in the “background.” Although this strategy can seem strange and complex, especially at first, it is in many ways easier to understand and control than multi-threaded programming.

    So instead of using threads, use non-blocking input/output: collect the sockets in a list and use an event loop with select.select to know which socket has data to read. Do that in a single thread.

    You could choose a python asynchronous networking framework like twisted to do that for you. That will save you a lot of headaches. Twisted’s code has been improved for years, and covers some corner cases you’ll take time to master.

    EDIT: Any existing async IO libraries (like Twisted) are python code. You could have written it yourself, but it has already been written for you. I don’t see why you wouldn’t use one of those libraries and write your own worst code instead, since you are a beginner. Networing IO is hard to get right.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think I got something workable after reading all the… May 11, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer Use php_sapi_name() Returns a lowercase string that describes the type… May 11, 2026 at 10:45 pm
  • Editorial Team
    Editorial Team added an answer sleep(float) blocks the main thread causing the UI to freeze… May 11, 2026 at 10:45 pm

Related Questions

I'm working on a multithreaded C++ application that is corrupting the heap. The usual
I have this legacy code base (Compaq PERL) , about 1500 lines of code,
I actually have two questions regarding the same problem but I think it is
I am working on a library that loads files (hfd5 - pytables) into an

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.