[django] Exception Value: DatabaseWrapper objects created in a thread can only be used in that same thread.
問題描述:
使用django框架實現的web後端服務,使用orm語句插入數據時報錯,報錯信息以下;git
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 11652github
報錯代碼位置如圖。web
問題分析:
項目本地調試是徹底ok的,部署到服務器上就出現這個問題。django
1.好了開始對代碼作各類排查,發現並無問題。後端
2.而後開始各類github搜索問題,這個問題出現的頻率也不高,最終找到兩個比較符合說法服務器
- gevent模塊開線程的衝突:https://blog.csdn.net/u014007037/article/details/86645862
- gunicorn起服務的衝突:https://github.com/benoitc/gunicorn/issues/927
問題解決
忽然想到,咱們項目開始使用uwsgi啓動服務的,也是在問題發生前換成了gunicorn部署,因而趕忙換回uwsgi來啓動服務,成功了!!app
查看了gunicorn的配置文件,發現裏面也調用了gevent的模塊。框架
這樣的話,大機率就是gevent的問題了。this