python - Using While condition inside of Thread's run() method -


I'm not sure that while true: run () method I have executed this code without the and status and it works fine. But I still want to know the purpose of using it. Any idea why it should be used or not?

QtCore .QThread): def __init __ (self, theQueue, parent = none): QtCore.QThread .__ init __ (self, parent) self.theQueue = theQ def def (self): while true: function = self.theQueue. Get () Self.sleep (1) self.theQueue.task_done () Thread = [] In the range I (1, 3): thread = TheThread (theQueue) threads.append (thread) thread.start () In (len (thread)): theQueue.put (i)

while True Loop means that your thread will always continue to block (in principle), task = self.theQueue.get () on the call. If you were using the threading.Thread object, it would hang your program and fail to exit, because Python threads will keep the main thread alive until they are running Before you start the daemon formula attribute in right. However, because you are using Qt-Threads, the program threads will still run out when running. When you do this, you will receive a warning:

  QThread: Thread is still running, while deleted: the thread is still running  

I have designed my threads to get a message only from the main thread, so no will recommend using loop loop, and call < Code> wait to ensure before the main thread goes out, exit from your main program Before threading.Thread.join on each thread object):

  class TheThread (QtCore.QThread): def __init __ (auto, theQueue, parent = None): QtCore.QThread .__ init __ (self, parent) self.theQueue = theQue def def (self): function = self.theQueue.get () itself. Sleep (1) print (work) self.theQueue.task_done () Thread = [] In the range I (1, 3): Thread = TheThread (theQueue) threads.append (thread) thread.start () for the category (Lane (thread)): theQueue.put (i) For threads in t: t.wait ()  

If you need , for any reason Use a watchdog to indicate the true loop, thread should they exit:

  class TheThread (QtCore.QThread): def __init __ (self, theQueue, parent = None): QtCore.QThread .__ init __ (self, parent) self.theQueue = theQueue def Own (self) to run to work for themselves (not self.theQueue.get, no): # There is no time to surrender. Sleep (1) print (work) self.theQueue.task_done () self.theQueue.task_done () thread = [] in category (1, 3): thread = TheThread (theQueue) threads.append thread (thread) thread.start ( ) For the category (lane (thread)): theQueue.put (i) # Leave in thread for T: Q for q in thread. Input (none): T.veet ()  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -