title: How to return value from function running by QThread and Queue
body: <p>Please explain how do we send/receive data from Thread managed by Queue....</p>
  
  <p>First I subclass 'QThread' defining its <code>run()</code> method which is started when <code>QThread</code>'s<code>.start()</code> is called:</p>
  
  <pre><code>class SimpleThread(QtCore.QThread):
      def __init__(self, queue, parent=None):
          QtCore.QThread.__init__(self, parent)      
          self.queue=queue        
      def run(self):
          while True:
              arg=self.queue.get() 
              self.fun(arg)    
              self.queue.task_done()
      def fun(self, arg):
          for i in range (3):
              print 'fun: %s'%i
              self.sleep(1)
          return arg+1
  </code></pre>
  
  <p>Then I declare two Thread instances (so only two CPU cores are taken) sending <code>self.queue</code> instance as an argument.</p>
  
  <pre><code>self.queue=queue.Queue()
  for i in range(2):
      thread=SimpleThread(self.queue)
      thread.start()
  </code></pre>
  
  <p>Now if I understand it correctly <code>thread.start()</code> is not starting anything. The real "start" happens only when I call <code>queue.put()</code>:</p>
  
  <pre><code>for arg in [1,2,3]: self.queue.put(arg)
  </code></pre>
  
  <p>This last line is what makes a "real" call. Aside from creating and starting <em>Queue item</em> <code>put()</code> allows to save any arbitrary value to each <em>Queue item</em>. <code>.put()</code> does several things at once: it creates, it starts, it moves the processing through the Queue and it allows to place a variable "inside" of the queue item (which later can be retrieved from inside of the function-processor: using Queue item's '.get()` method).</p>
  
  <p>But how do  I return the value from <code>fun()</code> function. A "regular" <code>fun()</code>'s  <code>return resultValue</code> doesn't work. And I can't use self.queue.put() method since this method aside from storing a data "creates" a new queue item...</p>
  
  <h1>EDITED LATER:</h1>
  
  <p>Here is slightly tweaked code (copy/pasted from another post) showing an approach on how to return a value from completed Thread. I am not sure if the the approach used here would work with QThread... please correct me if I am wrong:</p>
  
  <pre><code>import os, sys
  import threading
  import Queue
  
  def callMe(incomingFun, daemon=False):
      def execute(_queue, *args, **kwargs):
          result=incomingFun(*args, **kwargs)
          _queue.put(result)
  
      def wrap(*args, **kwargs):
          _queue=Queue.Queue()
          _thread=threading.Thread(target=execute, args=(_queue,)+args, kwargs=kwargs)
          _thread.daemon=daemon
          _thread.start()
          _thread.result_queue=_queue        
          return _thread
  
      return wrap
  
  @callMe
  def localFunc(x):
      import time
      x = x + 5
      time.sleep(5)
      return x
  
  thread=localFunc(10)
  
  # this blocks, waiting for the result
  result = thread.result_queue.get()
  print result
  </code></pre>