title: how to run a function called in a thread outside that thread in python
body: <p>I want to know the possibility to run a function in main thread, where the calling function is in another thread.</p>
  
  <p>Consider below example</p>
  
  <pre><code>from thread import start_new_thread
  
  def add(num1,num2):
  
      res = num1 + num2
      display(res)
  
  def display(ans):
      print ans
  
  thrd = start_new_thread(add,(2,5))
  </code></pre>
  
  <p>here i am calling <code>add()</code> in a new thread. which in turns calls <code>display()</code> i.e., display is also running in same thread.</p>
  
  <p>I want to know how to run <code>display()</code> outside that thread.</p>
  
  <p><strong>New code as per the answers below</strong></p>
  
  <p>If i am trying to accept input from user and print the result. Its asking for input only once but not repeatedly...</p>
  
  <p>from threading import Thread  # threading is better than the thread module
  from Queue import Queue</p>
  
  <p>q = Queue()  # use a queue to pass messages from the worker thread to the main thread</p>
  
  <pre><code>def add():
      num1=int(raw_input('Enter 1st num : '))
      num2=int(raw_input('Enter 2nd num : '))
      res = num1 + num2
      # send the function 'display', a tuple of arguments (res,)
      # and an empty dict of keyword arguments
      q.put((display, (res,), {}))
  
  def display(ans):
      print ('Sum of both two num is : %d ' % ans)
  
  thrd = Thread(target=add)
  thrd.start()
  
  while True:  # a lightweight "event loop"
  #    ans = q.get()
  #    display(ans)
  #    q.task_done()
      f, args, kwargs = q.get()
      f(*args, **kwargs)
      q.task_done()
  </code></pre>
  
  <p>and when i run the code, the result is as below</p>
  
  <p><strong>Current Result</strong></p>
  
  <pre><code>Enter 1st num : 5
  Enter 2nd num : 6
  Sum of both two num is : 11 
  </code></pre>
  
  <p><strong>Required Result</strong></p>
  
  <pre><code>Enter 1st num : 5
  Enter 2nd num : 6
  Sum of both two num is : 11 
  
  Enter 1st num : 8
  Enter 2nd num : 2
  Sum of both two num is : 10
  
  Enter 1st num : 15
  Enter 2nd num : 3
  Sum of both two num is : 18
  </code></pre>
  
  <p>where i need it to be asking for inputs after printing result every time, like below</p>