最近,我已经开始在我的项目中使用docker来运行以下内容
早些时候,设置调试器很容易,但是在使用了坞-撰写之后,我无法做到这一点。一旦启动调试器,它就会加载一段时间,然后在没有日志或错误的情况下自动停止。这是相关代码。
launch.json
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "localhost",
"port": 443
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
}
]
}docker-compose.yml
version: '3'
services:
db:
image: postgres:12
env_file: .env
environment:
- POSTGRES_DB=${DB_NAME}
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
ports:
- "5431:5432"
volumes:
- dbdata:/var/lib/postgresql/data
nginx:
image: nginx:1.14
ports:
- "443:443"
- "80:80"
volumes:
- ./config/nginx/:/etc/nginx/conf.d
- ./myapp/static:/var/www/myapp.me/static/
web:
restart: always
build: ./myapp
command: bash -c "
python manage.py collectstatic --noinput
&& python manage.py makemigrations
&& python manage.py migrate
&& gunicorn --certfile=/etc/certs/localhost.crt --keyfile=/etc/certs/localhost.key myapp.wsgi:application --bind 0.0.0.0:443 --reload"
expose:
- "443"
depends_on:
- db
- nginx
env_file:
- .env
volumes:
- ./myapp:/opt/myapp
- ./config/nginx/certs/:/etc/certs
- ./myapp/static:/var/www/myapp.me/static/
broker:
image: redis:alpine
expose:
- "6379"
celery:
build: ./myapp
command: celery -A myapp worker -l info
env_file:
- .env
volumes:
- ./myapp:/opt/myapp
depends_on:
- broker
- db
celery-beat:
build: ./myapp
command: celery -A myapp beat -l info
env_file:
- .env
volumes:
- ./myapp:/opt/myapp
depends_on:
- broker
- db
volumes:
dbdata:发布于 2020-10-15 19:16:22
最后我自己解决了这个问题。很少有人能摆脱这个问题。
您需要使用调试器并将其放在您的manage.py文件中,以便开始侦听端口。我做了这样的事
import debugpy
debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()
debugpy.breakpoint()除此之外,我们还需要将这个端口映射到主机内部的一个端口。为此,我们需要更改并在web服务中添加一行
ports:
- "5678:5678"我的launch.json看起来像这样
{
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"host": "0.0.0.0",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}/myapp",
"remoteRoot": "."
}
]
}
]
}注意:确保您的需求文件中有调试器,或者手动安装它。
发布于 2021-01-11 22:16:40
如果将调试器设置为模块,则可以使用环境变量打开或关闭调试器。我只是用这篇文章作为gunicorn/gunicorn调试解决方案的基础。
# debugger.py
from os import getenv
def initialize_flask_server_debugger_if_needed():
if getenv("DEBUGGER") == "True":
import multiprocessing
if multiprocessing.current_process().pid > 1:
import debugpy
debugpy.listen(("0.0.0.0", 10001))
print("⏳ VS Code debugger can now be attached, press F5 in VS Code ⏳", flush=True)
debugpy.wait_for_client()
print("� VS Code debugger attached, enjoy debugging �", flush=True)见禤浩焯的文章:VS代码中的烧瓶调试
https://stackoverflow.com/questions/64293300
复制相似问题