• <small id='YKymw'></small><noframes id='YKymw'>

        <bdo id='YKymw'></bdo><ul id='YKymw'></ul>

    1. <i id='YKymw'><tr id='YKymw'><dt id='YKymw'><q id='YKymw'><span id='YKymw'><b id='YKymw'><form id='YKymw'><ins id='YKymw'></ins><ul id='YKymw'></ul><sub id='YKymw'></sub></form><legend id='YKymw'></legend><bdo id='YKymw'><pre id='YKymw'><center id='YKymw'></center></pre></bdo></b><th id='YKymw'></th></span></q></dt></tr></i><div id='YKymw'><tfoot id='YKymw'></tfoot><dl id='YKymw'><fieldset id='YKymw'></fieldset></dl></div>
    2. <tfoot id='YKymw'></tfoot>
      1. <legend id='YKymw'><style id='YKymw'><dir id='YKymw'><q id='YKymw'></q></dir></style></legend>

        Python threading.Thread 只能使用私有方法 self.__Thread_stop() 停止

        时间:2023-09-29

          <i id='ePm4o'><tr id='ePm4o'><dt id='ePm4o'><q id='ePm4o'><span id='ePm4o'><b id='ePm4o'><form id='ePm4o'><ins id='ePm4o'></ins><ul id='ePm4o'></ul><sub id='ePm4o'></sub></form><legend id='ePm4o'></legend><bdo id='ePm4o'><pre id='ePm4o'><center id='ePm4o'></center></pre></bdo></b><th id='ePm4o'></th></span></q></dt></tr></i><div id='ePm4o'><tfoot id='ePm4o'></tfoot><dl id='ePm4o'><fieldset id='ePm4o'></fieldset></dl></div>
          • <legend id='ePm4o'><style id='ePm4o'><dir id='ePm4o'><q id='ePm4o'></q></dir></style></legend>
            • <bdo id='ePm4o'></bdo><ul id='ePm4o'></ul>

                  <tbody id='ePm4o'></tbody>
                <tfoot id='ePm4o'></tfoot>

                <small id='ePm4o'></small><noframes id='ePm4o'>

                  本文介绍了Python threading.Thread 只能使用私有方法 self.__Thread_stop() 停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个函数,它接受一个大数组 x,y 对作为输入,它使用 numpy 和 scipy 进行一些复杂的曲线拟合,然后返回一个值.为了尝试加快速度,我尝试使用两个线程将数据提供给使用 Queue.Queue .一旦数据完成.我试图让线程终止,然后结束调用进程并将控制权返回给 shell.

                  I have a function that accepts a large array of x,y pairs as an input which does some elaborate curve fitting using numpy and scipy and then returns a single value. To try and speed things up I am trying to have two threads that I feed the data to using Queue.Queue . Once the data is done. I am trying to have the threads terminate and then end the calling process and return control to the shell.

                  我试图理解为什么我必须在 threading.Thread 中使用私有方法来停止我的线程并将控制权返回给命令行.

                  I am trying to understand why I have to resort to a private method in threading.Thread to stop my threads and return control to the commandline.

                  self.join() 不会结束程序.重新获得控制权的唯一方法是使用私有停止方法.

                  The self.join() does not end the program. The only way to get back control was to use the private stop method.

                          def stop(self):
                              print "STOP CALLED"
                              self.finished.set()
                              print "SET DONE"
                              # self.join(timeout=None) does not work
                              self._Thread__stop()
                  

                  这是我的代码的近似值:

                  Here is an approximation of my code:

                      class CalcThread(threading.Thread):
                          def __init__(self,in_queue,out_queue,function):
                              threading.Thread.__init__(self)
                              self.in_queue = in_queue
                              self.out_queue = out_queue
                              self.function = function
                              self.finished = threading.Event()
                  
                          def stop(self):
                              print "STOP CALLED"
                              self.finished.set()
                              print "SET DONE"
                              self._Thread__stop()
                  
                          def run(self):
                              while not self.finished.isSet():
                                  params_for_function = self.in_queue.get()
                                  try:
                                      tm = self.function(paramsforfunction)
                                      self.in_queue.task_done()
                                      self.out_queue.put(tm)
                                  except ValueError as v:
                                      #modify params and reinsert into queue
                                      window = params_for_function["window"]
                                      params_for_function["window"] = window + 1
                                      self.in_queue.put(params_for_function)
                  
                      def big_calculation(well_id,window,data_arrays):
                              # do some analysis to calculate tm
                              return tm
                  
                      if __name__ == "__main__":
                          NUM_THREADS = 2
                          workers = []
                          in_queue = Queue()
                          out_queue = Queue()
                  
                          for i in range(NUM_THREADS):
                              w = CalcThread(in_queue,out_queue,big_calculation)
                              w.start()
                              workers.append(w)
                  
                          if options.analyze_all:
                                for i in well_ids:
                                    in_queue.put(dict(well_id=i,window=10,data_arrays=my_data_dict))
                  
                          in_queue.join()
                          print "ALL THREADS SEEM TO BE DONE"
                          # gather data and report it from out_queue
                          for i in well_ids:
                              p = out_queue.get()
                              print p
                              out_queue.task_done()
                              # I had to do this to get the out_queue to proceed
                              if out_queue.qsize() == 0:
                                  out_queue.join()
                                  break
                  # Calling this stop method does not seem to return control to the command line unless I use threading.Thread private method
                  
                          for aworker in workers:
                              aworker.stop()
                  

                  推荐答案

                  一般来说杀死修改共享资源的线程是个坏主意.

                  In general it is a bad idea to kill a thread that modifies shared resource.

                  除非在执行计算时释放 GIL,否则多线程中的 CPU 密集型任务在 Python 中比无用更糟糕.许多 numpy 函数do 发布 GIL.

                  CPU intensive tasks in multiple threads are worse than useless in Python unless you release GIL while performing computations. Many numpy functions do release GIL.

                  import concurrent.futures # on Python 2.x: pip install futures 
                  
                  calc_args = []
                  if options.analyze_all:
                      calc_args.extend(dict(well_id=i,...) for i in well_ids)
                  
                  with concurrent.futures.ThreadPoolExecutor(max_workers=NUM_THREADS) as executor:
                      future_to_args = dict((executor.submit(big_calculation, args), args)
                                             for args in calc_args)
                  
                      while future_to_args:
                          for future in concurrent.futures.as_completed(dict(**future_to_args)):
                              args = future_to_args.pop(future)
                              if future.exception() is not None:
                                  print('%r generated an exception: %s' % (args,
                                                                           future.exception()))
                                  if isinstance(future.exception(), ValueError):
                                      #modify params and resubmit
                                      args["window"] += 1
                                      future_to_args[executor.submit(big_calculation, args)] = args
                  
                              else:
                                  print('f%r returned %r' % (args, future.result()))
                  
                  print("ALL work SEEMs TO BE DONE")
                  

                  如果没有共享状态,您可以将 ThreadPoolExecutor 替换为 ProcessPoolExecutor.将代码放入您的 main() 函数中.

                  You could replace ThreadPoolExecutor by ProcessPoolExecutor if there is no shared state. Put the code in your main() function.

                  这篇关于Python threading.Thread 只能使用私有方法 self.__Thread_stop() 停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python中一个好的持久同步队列 下一篇:如何实现支持命名空间的 FIFO 队列

                  相关文章

                  <tfoot id='WYvGQ'></tfoot>
                    <bdo id='WYvGQ'></bdo><ul id='WYvGQ'></ul>

                    1. <small id='WYvGQ'></small><noframes id='WYvGQ'>

                    2. <i id='WYvGQ'><tr id='WYvGQ'><dt id='WYvGQ'><q id='WYvGQ'><span id='WYvGQ'><b id='WYvGQ'><form id='WYvGQ'><ins id='WYvGQ'></ins><ul id='WYvGQ'></ul><sub id='WYvGQ'></sub></form><legend id='WYvGQ'></legend><bdo id='WYvGQ'><pre id='WYvGQ'><center id='WYvGQ'></center></pre></bdo></b><th id='WYvGQ'></th></span></q></dt></tr></i><div id='WYvGQ'><tfoot id='WYvGQ'></tfoot><dl id='WYvGQ'><fieldset id='WYvGQ'></fieldset></dl></div>
                      <legend id='WYvGQ'><style id='WYvGQ'><dir id='WYvGQ'><q id='WYvGQ'></q></dir></style></legend>