Note that there are some explanatory texts on larger screens.

plurals
  1. POPython threading design question (Working with a method that blocks forever)
    primarykey
    data
    text
    <p><strong>UPDATED POST</strong> So, I found a solution to what I was doing it is to use <a href="https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread-in-python">Is there any way to kill a Thread in Python?</a></p> <p>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. </p> <p>Questions:</p> <p>1 ) Is it best to have One main thread, and two sub threads? </p> <p>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? </p> <p>Let me know whatever more information you may need.</p> <p>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.</p> <hr> <p><strong>ORIGINAL POST</strong> 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.</p> <p>Original code: </p> <pre><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 </code></pre> <p>I know the obvious locking issues exist, but is this the appropriate way to control a receive thread that waits forever for a message?</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload