python - exec() 调用中非常大的 python 函数定义使 Django 崩溃,但不会使直接执行的 Python 代码崩溃

标签 python django

我有一个非常大(约 40 万行)的 Python 函数,我试图通过 exec() 定义它。称呼。如果我运行以下 Python 脚本:

exec("""def blah()
# 400k lines of IF/THEN/ELSE
""", globals())
blah()
通过从命令行调用 Python,它工作正常。
但是,如果我在 Django 实例中执行相同操作,它会在没有任何错误消息或堆栈跟踪的情况下使服务器崩溃,我只能假设这是由于段错误造成的。
Django runserver 和上面的脚本都在同一个 Conda 环境中运行,并且都有无限的可用堆栈(通过在 Django 中打印出 resource.getrlimit 来确认)。
这是我的全部 ulimit -a输出:
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 515017
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) unlimited
cpu time               (seconds, -t) unlimited
max user processes              (-u) 4096
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
启动 Django 服务器的命令序列如下:
source activate <conda env name>
python manage.py runserver
这是导致崩溃的 shell 输入/输出:
(faf) [pymaster@t9dpyths3 faf]$ python manage.py runserver 9000
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 04, 2020 - 08:25:19
Django version 3.0.3, using settings 'faf.settings'
Starting development server at http://127.0.0.1:9000/
Quit the server with CONTROL-C.
[04/Aug/2020 08:25:25] "GET /projects/ HTTP/1.1" 200 13847
[04/Aug/2020 08:26:49] "PUT /projects/projectname/ HTTP/1.1" 200 76  # This event triggers the exec
(faf) [pymaster@t9dpyths3 faf]$

最佳答案

问题可能是由于 int(s), float(s) and others may cause segmentation fault
如前所述 here :
请尝试设置环境标志 PYTHONMALLOC=debug这可能允许您的代码运行而不会遇到段错误,如果您仍然遇到错误,您应该能够使用它来捕获它。

PYTHONMALLOC=debug python3 -X tracemalloc=10
您可能还想查看:faulthandler

This module contains functions to dump Python tracebacks explicitly, on a fault, after a timeout, or on a user signal. Call faulthandler.enable() to install fault handlers for the SIGSEGV, SIGFPE, SIGABRT, SIGBUS, and SIGILL signals. You can also enable them at startup by setting the PYTHONFAULTHANDLER environment variable or by using the -X faulthandler command line option.


添加这个是为了更清楚,因为它是相关的;以下内容取自 Darrrrrren 提供的答案,是使故障处理程序在线程 django 应用程序上运行的调整:

So I was able to get a stack trace by initializing Python with faulthandler, but additionally I had to run manage.py runserver --nothreading --noreload - for some reason if you do not disable threading with Django, even faulthandler will not print a stack trace.

关于python - exec() 调用中非常大的 python 函数定义使 Django 崩溃,但不会使直接执行的 Python 代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63193250/

相关文章:

python - 从图像本地目录创建tensorflow数据集

Django序列化器属性错误: 'unicode' object has no attribute 'isoformat'

python - 如何从 Django 模型中获取 ID

Python库问题

Python CFFI 将结构转换为字典

python - 如何查找url多个参数并更改值?

python - 使用 vlines 在 Python 中自定义线条样式

python - 使用自己的字段在 MongoDB 中进行过滤

python - 带有 nginx 反向代理的 django SECURE_SSL_REDIRECT