title: How to pass multiprocessing.Pool instance to apply_async callback function?
body: <p>Here is my prime factorization program,i added a callback function in <code>pool.apply_async(findK, args=(N,begin,end))</code>,a message prompt out <code>prime factorization is over</code> when factorization is over,it works fine.</p>
  
  <pre><code>import math
  import multiprocessing 
  
  def findK(N,begin,end):
      for k in range(begin,end):
          if N% k == 0:
              print(N,"=" ,k ,"*", N/k)
              return True
      return False
  
  
  def prompt(result):
      if result:
          print("prime factorization is over")
  
  
  def mainFun(N,process_num):
      pool = multiprocessing.Pool(process_num)
      for i in range(process_num):
          if i ==0 :
              begin =2
          else:
              begin = int(math.sqrt(N)/process_num*i)+1
          end = int(math.sqrt(N)/process_num*(i+1))
          pool.apply_async(findK, args=(N,begin,end) , callback = prompt)    
      pool.close()
      pool.join()    
  
  if __name__ == "__main__":
      N = 684568031001583853
      process_num = 16
      mainFun(N,process_num)
  </code></pre>
  
  <p>Now i want to change the callback function in apply_async,to change prompt into a shutdown function to kill all other process.</p>
  
  <pre><code>def prompt(result):
      if result:
          pool.terminate()
  </code></pre>
  
  <p>The pool instance is not defined in prompt scope or passed into prompt.<br>
  <code>pool.terminate()</code>  can't work in prompt function.<br>
  How to pass  multiprocessing.Pool instance to  apply_async'callback function ?<br>
  (I have made it done in class format,just to add a class method and call self.pool.terminate can kill all other process,
  how to do the job in function format?)    </p>
  
  <p>if not set pool as global variable, can pool be passed into callback function?</p>