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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:51:14+00:00 2026-05-14T00:51:14+00:00

I have very little experience working with sockets and multithreaded programming so to learn

  • 0

I have very little experience working with sockets and multithreaded programming so to learn more I decided to see if I could hack together a little python socket server to power a chat room. I ended up getting it working pretty well but then I noticed my server’s CPU usage spiked up over 100% when I had it running in the background.

Here is my code in full: http://gist.github.com/332132

I know this is a pretty open ended question so besides just helping with my code are there any good articles I could read that could help me learn more about this?

My full code:

import select 
import socket 
import sys
import threading 
from daemon import Daemon

class Server: 
def __init__(self): 
    self.host = '' 
    self.port = 9998 
    self.backlog = 5 
    self.size = 1024 
    self.server = None 
    self.threads = []
    self.send_count = 0

def open_socket(self): 
    try: 
        self.server = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) 
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.bind((self.host,self.port)) 
        self.server.listen(5) 
        print "Server Started..."
    except socket.error, (value,message): 
        if self.server: 
            self.server.close() 
        print "Could not open socket: " + message 
        sys.exit(1) 

def remove_thread(self, t):
    t.join()

def send_to_children(self, msg):
    self.send_count = 0
    for t in self.threads:
        t.send_msg(msg)
    print 'Sent to '+str(self.send_count)+" of "+str(len(self.threads))

def run(self): 
    self.open_socket() 
    input = [self.server,sys.stdin] 
    running = 1 
    while running: 
        inputready,outputready,exceptready = select.select(input,[],[]) 

        for s in inputready: 
            if s == self.server: 
                # handle the server socket 
                c = Client(self.server.accept(), self) 
                c.start() 
                self.threads.append(c)
                print "Num of clients: "+str(len(self.threads))

    self.server.close() 
    for c in self.threads: 
        c.join() 

class Client(threading.Thread): 
def __init__(self,(client,address), server): 
    threading.Thread.__init__(self) 
    self.client = client 
    self.address = address 
    self.size = 1024
    self.server = server
    self.running = True

def send_msg(self, msg):
    if self.running:
        self.client.send(msg)
        self.server.send_count += 1

def run(self):
    while self.running: 
        data = self.client.recv(self.size) 
        if data:
            print data
            self.server.send_to_children(data)
        else: 
            self.running = False
            self.server.threads.remove(self)
            self.client.close()

"""
Run Server
"""

class DaemonServer(Daemon):
def run(self):
    s = Server()
    s.run()

if __name__ == "__main__": 
d = DaemonServer('/var/servers/fserver.pid')
if len(sys.argv) == 2:
    if 'start' == sys.argv[1]:
        d.start()
    elif 'stop' == sys.argv[1]:
        d.stop()
    elif 'restart' == sys.argv[1]:
        d.restart()
    else:
        print "Unknown command"
        sys.exit(2)
    sys.exit(0)
else:
    print "usage: %s start|stop|restart" % sys.argv[0]
    sys.exit(2)
  • 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-14T00:51:14+00:00Added an answer on May 14, 2026 at 12:51 am

    There are several possible race conditions in your code, but they would threaten correctness rather than performance: fixing them e.g. by locking would definitely not improve performance.

    Rather, I’d focus on what good you think those threads are doing, at all — since the core of your code is a select.select call, why not focus on that… and a totally asynchronous, thus more effective… server, instead of bouncing some tasks off to threads which basically are just overhead. Read when some input is ready (as you’re doing), write when some socket is ready for output, &c — it’s simpler and faster than the current mix of threads and async.

    Programming async servers directly on top of select.select is quite a low-level approach, and while instructive it’s not really suitable for production. Consider using the asyncore and asynchat modules of the Python standard library for a modestly higher abstraction level, or the twisted third-party package for a much higher boost (including the ability to implement the underlying “Reactor” design pattern by more effective means than old select — there’s poll, kqueues, etc, depending on what OS you’re on, and Twisted can let you choose the implementation depending on your platform, while keeping the same Reactor interface).

    I think I cover these various possibilities decently, if concisely, in the “server-side sockets” chapter of Python in a Nutshell 2nd Ed — which you can get for free online by getting a trial subscription to O’Reilly’s “Safari Online” site, or (illegally;-) by finding and using one of the many pirate sites hosting pirate copies of books (assuming of course you don’t want to spend money for it by getting it “all legal and proper”;-). I think you can freely download a zipfile with all example code from O’Reilly’s website, anyway.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • 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 The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

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.