在 pycharm 中使用多处理时如何调试

How to debug when using multiprocessing in pycharm

我正在 pycharm 社区版中使用 anaconda2 调试多进程程序。 它有几个后台工作进程。工作进程将检查输入队列以在接收到任务之前不休眠地检索任务。事实上,我只对主进程感兴趣。但是pycharm调试器老是进入子进程,好像主进程一直没有工作,任务一直没有发出来。如何使调试器脱离子进程? 工作子进程如下所示:

class ILSVRC_worker:

...

def run(self):
    cfg_parser = ConfigParser.ConfigParser()
    cfg_parser.read(self.cfg_path)
    data_factory = ILSVRC_DataFactory(cfg_parser)
    logger = mp.log_to_stderr(logging.INFO)
    while True:
        try:
            annotation_path = self.que_in.get(True,0.1)
        except Queue.Empty:
            continue
        if annotation_path is None:
            # to exit the subprocess
            logger.info('exit the worker process')
            break
        ...

我可以想出两种方法来实现这一点,但不幸的是,我认为社区版无法实现。

  • 如果您有进程的 PID,您可以尝试使用“工具”>“附加到进程..”功能附加到它(我不知道社区版是否提供该功能)。如果您使用 Pool,这会很困难,因为您不知道该作业分配给哪个进程。
  • 另一种方法是使用远程调试器并在调度的 python 进程中连接到它。这仅在专业版中可用

我最终在没有任何多处理的情况下测试了我的代码