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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:44:56+00:00 2026-05-20T12:44:56+00:00

UPDATED POST So, I found a solution to what I was doing it is

  • 0

UPDATED POST
So, I found a solution to what I was doing it is to use Is there any way to kill a Thread in Python?

I would like to get some validation on design. We have a fairly large system, and one components of the system is the communication component. It is in charge of two things, one sending messages, and two queuing any messages that get received (to file,db, queue, etc.) I am receiving the message via a provided library that basically can just wait forever for a message to arrive.

Questions:

1 ) Is it best to have One main thread, and two sub threads?

2) Is it better to allow the Receive thread to just block forever until a message arrives? Or should I allow for it to timeout(which is an exception), and just continue to loop?

Let me know whatever more information you may need.

Right now I basically have a receive thread, and a main thread (which also handles the sending), which you can see below. The only thing not included below is the fect I can now call “terminate” on MessageReceiver and it will exit.


ORIGINAL POST
I am trying to wrap a thread around some receiving logic in python. Basically we have an app, that will have a thread in the background polling for messages, the problem I ran into is that piece that actually pulls the messages waits forever for a message. Making it impossible to terminate… I ended up wrapping the pull in another thread, but I wanted to make sure there wasn’t a better way to do it.

Original code:

class Manager:
   def __init__(self):
        receiver = MessageReceiver()
        receiver.start()
        #do other stuff sending/etc...

class MessageReceiver(Thread):
   receiver = Receiver()

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


   def run(self):           
       #stop is a flag that i use to stop the thread...
       while(not stopped ):
          #can never stop because pull below blocks
          message  = receiver.pull()
          print "Message" + message   

I know the obvious locking issues exist, but is this the appropriate way to control a receive thread that waits forever for a message?

One thing I did notice was this thing eats 100% cpu while waiting for a message…
**If you need to see the stopping logic please let me know and I will post.

  • 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-20T12:44:56+00:00Added an answer on May 20, 2026 at 12:44 pm

    Since there was never a real answer to this question I will summarize:

    1. One Main thread which handles the sending, and spawns a receiving thread
    2. Block forever, exceptions are costly, so there is no need to let it time out.

    In order to stop the thread I basically set the stop flag then terminate the underlying connection in a “terminate” message of the parent. In return which causes an exception in the child.

    In the exception handling block I then check the ConnectionException, if the”stop” flag has been set, I gracefully stop the receiving thread, if it was an ungraceful exit, I will then notify the parent thread.

    Hope this helps, this was a super annoying problem, but I am glad it is over.

    class Manager:
       def __init__(self):
          self.receiver = MessageReceiver(shutdown_hook = self.shutdown_hook)
          self.receiver.start()
          #do other stuff sending/etc...
       def terminate(self):
          self.receiver.stop()
          self.receiver.close()
    
       def shutdown_hook(self, t_id, child):
           print '%s - Unexpected thread shutdown, handle this.. restart thread?' % str(t_id))
    
    class MessageReceiver(Thread):
    
    
       def __init__(self,shutdown_hook = None):
         Thread.__init__(self)
         self.receiver = Receiver()
         self.shutdown_hook = shutdown_hook 
    
    
       def run(self):           
         #stop is a flag that i use to stop the thread...
         while(not stopped ):
          try:
            message  = self.receiver.pull()
            print "Message" + message   
          except ConnectionException as e:
             if stopped:
                #we are in the process of stopping
                pass
             else:
                self.shutdown_hook(self.iden, e)
                break
          finally:
                receiver.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the JS: $('#createReview').click(function () { CKEDITOR.instances['ReviewText'].updateElement(); $.ajax({ type: 'POST', cache: false, url:
Here is the JS: $('#createReview').click(function () { CKEDITOR.instances['ReviewText'].updateElement(); $.ajax({ type: 'POST', cache: false, url:
Update : I've found a workaround. If I submit a dummy form field along
My ASP.NET MVC 2 application uses Entity Framework 4.0 for the data model. Following
here is the problem, today I had a bad time debugging my project, the
I am using ASP.NET MVC2 and have a problem. After Log off I manually
I am in the early stages of developing an Azure web project which will
Setting up paperclip with S3 in my linux dev environment was a snap --
I have created a .c file which is being converted to a .o file
I am using T4MVC to redirect to another action return RedirectToAction(MVC.MyController.MyAction());. In result it

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.