简体   繁体   English

vscode中的python - 无法让多处理工作

[英]python in vscode - can't get multiprocessing to work

I'm not new to Python, but pretty new to working with multiprocessing in Python.我对 Python 并不陌生,但对在 Python 中使用多处理非常陌生。 I prefer working in vscode, but it's acting very weird when I try to run a simple multiprocessing example.我更喜欢在 vscode 中工作,但是当我尝试运行一个简单的多处理示例时,它的表现非常奇怪。

I'm running Python 3.7.2 and VSCode version 1.43.0 on Windows.我在 Windows 上运行 Python 3.7.2 和 VSCode 版本 1.43.0。

Here's my files.这是我的文件。 First the caller:首先是调用者:

## multi_runner.py

import multiprocessing
import multi_worker
import time

if __name__ == '__main__':
    print('Starting the jobs.')
    jobs = []
    for i in range(5):
        p = multiprocessing.Process(
            target=multi_worker.worker
        )
        jobs.append(p)
        p.start()
    while any(job.is_alive() for job in jobs):
        print('sleeping')
        time.sleep(1)
    print('All done.')

And the worker:和工人:

## multi_worker.py

import multiprocessing
import sys

def worker():
    """worker function"""
    print(f'Worker')
    return

When I run this at the command line, everything works the way you'd expect:当我在命令行上运行它时,一切都按照您期望的方式工作:

(venv) C:\multiexample>python multi_runner.py
Starting the jobs.
sleeping
Worker
Worker
Worker
Worker
Worker
All done.

But when I run it from within the VSCode debugger, it seems like it never yields to the workers, and I have to kill it to end it:但是当我从 VSCode 调试器中运行它时,它似乎永远不会屈服于工作人员,我必须杀死它才能结束它:

(venv) PS C:\multiexample>  ${env:PTVSD_LAUNCHER_PORT}='52842'; & 'c:\multiexample\venv\Scripts\python.exe' 'c:\Users\myuser\.vscode\extensions\ms-python.python-2020.2.64397\pythonFiles\lib\python\new_ptvsd\wheels\ptvsd\launcher' 'c:\multiexample\multi_runner.py'
    Starting the jobs.
    sleeping
    sleeping
    sleeping
    sleeping
    sleeping
    sleeping
    sleeping
    sleeping
    sleeping
    <... forever ...>

Here's the default launch.json file I'm using:这是我正在使用的默认 launch.json 文件:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        }
    ]
}

Is there a way to make this work in VSCode?有没有办法在 VSCode 中完成这项工作?

您将获得 Pycharm 的社区版,这将帮助您进行调试而无需配置 VS Code –

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM