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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:49:45+00:00 2026-06-07T04:49:45+00:00

I am writing an application to collect UDP messages and process them every 1sec.

  • 0

I am writing an application to collect UDP messages and process them every 1sec.

Application prototype looks like:

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import threading
import time

class UdpListener(DatagramProtocol):

    messages = []

    def datagramReceived(self, data, (host, port)):
        self.messages.append(data)

class Messenger(threading.Thread):

    listener = None

    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        while True:
            time.sleep(1)
            recivedMessages = self.listener.messages
            length = len(recivedMessages)
            messagesToProccess = recivedMessages[0:length]
            #doSomethingWithMessages(messagesToProccess)
            del self.listener.messages[0:length]
            print(length)

listener = UdpListener()

messenger = Messenger()
messenger.listener = listener
messenger.start()

reactor.listenUDP(5556, listener)
reactor.run()

I am not sure if I can easily remove beginning values from list (del self.listener.messages[0:length]) without any risk that incoming message changes list and application crashes.

Update – version with lock

class Messenger(threading.Thread):

listener = None
lock = threading.Lock()

def __init__(self):
    threading.Thread.__init__(self)

def run(self):
    while True:
        time.sleep(1)
        recivedMessages = self.listener.messages
        self.lock.acquire()
        try:
            length = len(recivedMessages)
            messagesToProccess = recivedMessages[0:length]
            del self.listener.messages[0:length]
        except Exception as e:
            raise e
        finally:
            self.lock.release()

        #doSomethingWithMessages(messagesToProccess)
        print(length)
  • 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-07T04:49:47+00:00Added an answer on June 7, 2026 at 4:49 am

    Your code isn’t thread-safe, no. You’d need to have a lock around messages.

    However, you don’t need a thread here. Why not do this?

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor
    
    class UdpListener(DatagramProtocol):
        callingLater = False
    
        messages = []
    
        def process(self):
            doSomethingWithMessages(self.messages)
            self.messages = []
            self.callingLater = False
    
        def datagramReceived(self, data, (host, port)):
            self.messages.append(data)
            if not self.callingLater:
                reactor.callLater(1.0, self.process)
                self.callingLater = True
    
    listener = UdpListener()
    
    reactor.listenUDP(5556, listener)
    reactor.run()
    

    UPDATE: Here is how the original version would work with a lock, for educational purposes only. Note that this is not as efficient and also more prone to bugs. EDIT: Separated all the message logic out into UdpListener so the classes using it don’t need to know its gooey internal details.

    from twisted.internet.protocol import DatagramProtocol
    from twisted.internet import reactor
    import threading
    import time
    
    class UdpListener(DatagramProtocol):
        message_lock = threading.Lock()
        messages = []
    
        def datagramReceived(self, data, (host, port)):
            with self.message_lock:
                self.messages.append(data)
    
        def getAndClearMessages(self):
            with self.message_lock:
                res = self.messages
                self.messages = []
            return res
    
    class Messenger(threading.Thread):
    
        listener = None
    
        def __init__(self):
            threading.Thread.__init__(self)
    
        def run(self):
            while True:
                time.sleep(1)
                recivedMessages = self.listener.getAndClearMessages()
                length = len(recivedMessages)
                #doSomethingWithMessages(recivedMessages)
                print(length)
    
    listener = UdpListener()
    
    messenger = Messenger()
    messenger.listener = listener
    messenger.start()
    
    reactor.listenUDP(5556, listener)
    reactor.run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a django application to collect and analyse log files from various different
I am writing an application that will move data from one database to another
I am writing an application in Qt using python that will process a txt
I am writing a service to collect location readings while my application is running
Background : I'm writing an application in C# (.NET 3.5), that looks at multiple
Writing an application with command line interface and I would like to know at
In an application at our company we collect statistical data from our servers (load,
i'm writing application like social network where in my application can show status update
I am writing application in Linux using C, pthreads and sockets. This will be
I am writing application on ActionScript for Android using Adobe AIR with native extentions.

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.