[django] Exception Value: DatabaseWrapper objects created in a thread can only be used in that same thread.

问题描述:

使用django框架实现的web后端服务,使用orm语句插入数据时报错,报错信息如下;

image-20200326101024514

django.db.utils.DatabaseError: DatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias ‘default’ was created in thread id 8576 and this is thread id 11652

报错代码位置如图。

image-20200326101256272

问题分析:

项目本地调试是完全ok的,部署到服务器上就出现这个问题。

1.好了开始对代码做各种排查,发现并没有问题。

2.然后开始各种github搜索问题,这个问题出现的频率也不高,最终找到两个比较符合说法

  • gevent模块开线程的冲突:https://blog.csdn.net/u014007037/article/details/86645862
  • gunicorn起服务的冲突:https://github.com/benoitc/gunicorn/issues/927

问题解决

突然想到,我们项目开始使用uwsgi启动服务的,也是在问题发生前换成了gunicorn部署,于是赶紧换回uwsgi来启动服务,成功了!!

查看了gunicorn的配置文件,发现里面也调用了gevent的模块。

image-20200326113005253

这样的话,大概率就是gevent的问题了。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐