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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:07:48+00:00 2026-06-19T01:07:48+00:00

Sorry if this question is too basic – this is the first time that

  • 0

Sorry if this question is too basic – this is the first time that I try using multithreaded sockets in Python.

Basically, I’m trying to write code that gets data that’s being received by a UDPServer socket; the socket itself is running inside of a thread. In order to make the data accessible to the main thread, I’m using Thread local storage (it seems that’s the correct way to do it based on everything that I read). The code below is my first attempt to make it work, but the variable doesn’t seem to be properly updated. Any idea about what could be happening?

EDIT: see below for a working example

Server:

import socket
import threading
import SocketServer

data = threading.local()

class UDPHandler(SocketServer.BaseRequestHandler):

    def handle(self):
        data.outputString = self.request[0].strip()

class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):

    def __init__(self, serverAddress, handlerClass):
        SocketServer.UDPServer.__init__(self, serverAddress, handlerClass)
        data.outputString = ""

if __name__ == "__main__":

    ReceiverSocket = ThreadedUDPServer(("localhost",11111), UDPHandler)
    ServerThread = threading.Thread(target=ReceiverSocket.serve_forever)
    ServerThread.daemon = True
    ServerThread.start()

    while 1:
        if data.outputString:
            print data.outputString
            data.outputString = ""

Client:

import socket
import sys

HOST, PORT = "localhost", 11111
data = " ".join(sys.argv[1:])

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

sock.sendto(data + "\n", (HOST, PORT))

Thanks!!

  • 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-19T01:07:50+00:00Added an answer on June 19, 2026 at 1:07 am

    Made it work. And yes – Thread local has nothing to do with this… 🙂

    I have set a global variable and defined it as global in each function modifying it (as per this very helpful answer)

    import socket
    import threading
    import SocketServer
    
    data = ""
    
    class UDPHandler(SocketServer.BaseRequestHandler):
    
        def handle(self):
            global data
            data = self.request[0].strip()
    
    class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
        pass
    
    if __name__ == "__main__":
    
        ReceiverSocket = ThreadedUDPServer(("localhost",11111), UDPHandler)
        ServerThread = threading.Thread(target=ReceiverSocket.serve_forever)
        ServerThread.start()
    
        while 1:
            if data:
                print data
                data = ""
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my first question sorry if this is too basic. I followed the tutorial Beginning
Sorry for this question but I have spent too much time to find answer.
I'm sorry if this question is too basic I have two SWT TreeViewer's in
Sorry if this question is too vague, but I'd rather not muddy it's point
Sorry if this question will sound too chaotic, feel free to edit it. I
Maybe this is a very basic question but i am a beginner in python
Sorry if this question is too silly or neurotic... But I can't figure it
I am a C++ beginner, so sorry if the question is too basic. I
First of all, let me apologize if this question is too broad. I'm looking
I'm new to programming, and hope this question isn't too basic. I've searched the

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.