RuntimeError:populate()不可重入

时间:2018-03-15 08:11:00

标签: django python-3.x virtualhost

我得到了一个基于Django的遗留项目作为后端和前面的Angularjs。它已部署并正在运行,但我根本没有文档,所以我不得不猜测如何在本地部署它,系统如何工作以及那些。

现在,我已被要求在预生产环境中进行设置,所以我尝试这样做,我从生产服务器复制了所有配置并根据需要进行了更改以适应新环境

[Thu Mar 15 07:08:53.612256 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429] mod_wsgi (pid=13884): Target WSGI script '/opt/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Thu Mar 15 07:08:53.612336 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429] mod_wsgi (pid=13884): Exception occurred processing WSGI script '/opt/mysite/mysite/wsgi.py'.
[Thu Mar 15 07:08:53.612539 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429] Traceback (most recent call last):
[Thu Mar 15 07:08:53.612602 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]   File "/opt/mysite/mysite/wsgi.py", line 19, in <module>
[Thu Mar 15 07:08:53.612611 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]     application = get_wsgi_application()
[Thu Mar 15 07:08:53.612624 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]   File "/usr/local/lib/python3.6/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Thu Mar 15 07:08:53.612632 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]     django.setup(set_prefix=False)
[Thu Mar 15 07:08:53.612643 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]   File "/usr/local/lib/python3.6/dist-packages/django/__init__.py", line 24, in setup
[Thu Mar 15 07:08:53.612649 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]     apps.populate(settings.INSTALLED_APPS)
[Thu Mar 15 07:08:53.612659 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]   File "/usr/local/lib/python3.6/dist-packages/django/apps/registry.py", line 81, in populate
[Thu Mar 15 07:08:53.612666 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429]     raise RuntimeError("populate() isn't reentrant")
[Thu Mar 15 07:08:53.612693 2018] [wsgi:error] [pid 13884:tid 140222719059712] [remote 212.170.177.164:49429] RuntimeError: populate() isn't reentrant

我尝试访问该文件(registry.py)并对其进行编辑,以便我可以看到内部发生了什么,但它不会在Django控制台上打印任何内容,也不会在文件中打印。< / p>

以下是我的配置文件:

/etc/apache2/sites-enabled/dev.my_domain.com.conf

WSGIPythonPath /opt/mysite:/home/my_user/.local/lib/python3.6/site-packages
<VirtualHost *:80>
        WSGIScriptAlias /backend /opt/mysite/mysite/wsgi.py

        <Directory /opt/mysite/mysite/>
        Require all granted
        </Directory>

        WSGIDaemonProcess mysite python-path=/opt/mysite:/home/my_user/.local/lib/python3.6/site-packages
        WSGIProcessGroup mysite

        Alias /media/ /opt/mysite/media/
        Alias /static/ /opt/mysite/static/

        <Directory /opt/mysite/media>
        Require all granted
        </Directory>

        <Directory /opt/mysite/static>
        Require all granted
        </Directory>


    ServerName dev.my_domain.com
    ServerAdmin admin@my_domain.com
    DocumentRoot /var/www/html/dev.my_domain.com
    ErrorLog /var/log/apache2/virtual.host.error.log
    CustomLog /var/log/apache2/virtual.host.access.log combined
    LogLevel warn
</VirtualHost>

/var/www/html/dev.mydomain.com/.htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule ^.*$ https://%1/$1 [R=301,L]

RewriteEngine On

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteCond %{REQUEST_URI} !.*\.(css¦js|html|png)

RewriteRule ^(.*) /index.html

Django服务器可以从控制台正常运行,没有任何错误,但请求永远不会到达它。

我在Ubuntu 17.04上运行Python3.6和Django 1.9.5(与prod服务器相同)。 .htaccess已经从prod服务器原样复制。防火墙没有阻止Django的端口8000。没有跑过virtualenvs

有关如何面对此问题的任何建议吗?

1 个答案:

答案 0 :(得分:0)

在我的情况下,我从一个简单的$ python manage.py shell中也遇到了类似的错误。我无法从搜索 RuntimeError(“ populate()is not reentrant”)中找到任何帮助,所以我终于着手在调试器中进行搜索。

以我为例,事实证明Oracle对我的环境变量不满意。

如果这不是您的问题,因为您检查了一下,那么以下一些步骤可能会有所帮助。

摘要

我必须到达代码中发生以下情况的正确位置。这是在它完全炸毁之前,因此不幸的是,您不能仅将调试器运行到最终点并点击[w]here以查看跟踪: (Pdb) n ImproperlyConfigured: Improper...ectory',) <<- that's it, folks

要显示该隐式缩写消息是什么,[w]here命令显示(在堆栈末尾): (Pdb) w [...] -> raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e) (Pdb) 因此,事实证明我忘了加载一些环境变量,而Oracle无法加载。 叹气。

详细信息

如果您决定使用此方法,建议您这样设置断点(您的行号可能会有所不同-版本会有所变化):

  • 在调试器中运行应用程序,并使其失败。

    (env) [prompt] $ python -m pdb manage.py shell 
    > <fullpath>/manage.py(2)<module>()
    -> import os
    (Pdb) c
    Traceback (most recent call last):
      [snip]
      File "<path>/django/__init__.py", line 27, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "<path>/django/apps/registry.py", line 78, in populate
        raise RuntimeError("populate() isn't reentrant")
    RuntimeError: populate() isn't reentrant
    Uncaught exception. Entering post mortem debugging
    Running 'cont' or 'step' will restart the program 
    > <path>/django/apps/registry.py(78)populate()
    -> raise RuntimeError("populate() isn't reentrant")
    (Pdb) 
    
  • 请注意,它在registry.py中失败(就我而言)。在这里时,请在registry.py中环顾四周。您正在寻找调用import_models()方法的行。就我而言,第104行将我带到附近地区:

    (Pdb) l 104
     99                 if duplicates:
    100                     raise ImproperlyConfigured(
    101                         "Application names aren't unique, "
    102                         "duplicates: %s" % ", ".join(duplicates))
    103  
    104                 self.apps_ready = True
    105  
    106                 # Phase 2: import models modules.
    107                 for app_config in self.app_configs.values():
    108                     app_config.import_models()
    109  
    (Pdb) l
    110                 self.clear_cache()
    111  
    112                 self.models_ready = True
    113  
    114                 # Phase 3: run ready() methods of app configs.
    115                 for app_config in self.get_app_configs():
    116                     app_config.ready()
    117  
    118  ->             self.ready = True
    119  
    120         def check_apps_ready(self):
    (Pdb) 
    

请注意,在我的示例中,您要查找的代码在第108行。那就是您要设置断点的地方。

  • 重新启动(pdb不会使用简单的restart重新初始化所有内容,因此请退出并重新启动):

    (Pdb) q
    [snip]
    $ python -m pdb manage.py shell # just like you did before
    
  • 现在设置该断点:

    b django/apps/registry.py:108
    Breakpoint 1 at <path>/django/apps/registry.py:108
    (Pdb) 
    

请记住:您的行号可能不同。 [c]ontinue到断点。

  • 现在需要耐心的部分出现了:打[n]ext直到得到看起来不错的东西,好吧,可疑是唯一的单词:

    (Pdb) n
    ImproperlyConfigured: Improper...ectory',)  <<-- yeah, that bit, there
    > <path>/django/apps/registry.py(108)populate()
    -> app_config.import_models()
    
  • 现在是时候在调试器中命中[w]here了:

    (Pdb) w
    [snip the long traceback]
    [You'll see the line you're on now in the middle:]
    > <path>/django/apps/registry.py(108)populate()
    [snip]
    -> raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e)
    (Pdb) 
    
  • raise行是导致它爆炸的原因。