为什么python apply_async不立即返回?

时间:2019-06-17 04:09:06

标签: python python-multiprocessing

我正在尝试使用apply_async运行一些代码。 并把logging.info()检查它是否按预期工作。 (预期的非阻塞操作)

我的结果是,apply_async用作阻止操作而不是非阻止操作。

请让我知道为什么。

class DetectThread(QThread):


    def run(self):
        pool = Pool(processes=4)
"""
...... some ops. here
"""
        cap = cv2.VideoCapture(0) 
        while True:
            logging.info("while - begin ")
            ret, frame = cap.read()
            proc = pool.apply_async(self.function_to_call, args=())
            idx_frame = idx_frame + 1
            logging.info("  apply_async - end ")
"""
...... some ops. here
"""
            logging.info("while - end ")



    def function_to_call(self, some args):
        logging.info("    function_to_call - start ")

        orig_frame = frame.copy()
"""
...... some ops. here
"""
        idx = idx + 1
        logging.info("    function_to_call - end ")
        return idx


我的结果是,

   1458 Dummy-1    while - begin 
   1642 Dummy-1      apply_async - start 
   1683 Dummy-1        function_to_call - start 
   1763 Dummy-1        function_to_call - end 
   1765 Dummy-1      apply_async - end 
   1872 Dummy-1    while - end 
   1872 Dummy-1    while - begin 
   2466 Dummy-1      apply_async - start 
   2488 Dummy-1        function_to_call - start 
   2553 Dummy-1        function_to_call - end 
   2555 Dummy-1      apply_async - end 
   2594 Dummy-1    while - end

0 个答案:

没有答案