【发布时间】:2020-03-18 15:47:44
【问题描述】:
我对 Python 并不陌生,但对在 Python 中使用多处理还是很陌生。我更喜欢在 vscode 中工作,但是当我尝试运行一个简单的多处理示例时,它的表现非常奇怪。
我在 Windows 上运行 Python 3.7.2 和 VSCode 版本 1.43.0。
这是我的文件。首先调用者:
## 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.')
还有工人:
## multi_worker.py
import multiprocessing
import sys
def worker():
"""worker function"""
print(f'Worker')
return
当我在命令行中运行它时,一切都会按照您的预期进行:
(venv) C:\multiexample>python multi_runner.py
Starting the jobs.
sleeping
Worker
Worker
Worker
Worker
Worker
All done.
但是当我从 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 ...>
这是我使用的默认 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"
}
]
}
有没有办法在 VSCode 中完成这项工作?
【问题讨论】:
-
VS Code 适用于很多事情。对于某些特定任务,请使用 Pycharm 等专业编辑器。
-
请尝试github.com/microsoft/ptvsd/issues/2104 列出的最新调试器,看看是否能解决问题。
标签: python visual-studio-code python-multiprocessing