问题描述
在使用 rpy2 时在 Django 中看到一些奇怪的行为。
rfn.py:
from rpy2.robjects import r as R
import rpy2.robjects as robjects
import json
from .models import TestModel
# 4-5 varIoUs R functions
R('''
library(someLib)
someFunc <- function() {
}
''')
class Spectrascores():
def __init__(self,form):
pass
# A number of other function which when commented out
# do not change the error being produced.
# E.g.,def init():
pass
views.py:
from .rfn import Spectrascores
def view_cosine(request):
if request.method == 'POST':
form = ViewCosineForm(request.POST,request.FILES)
if form.is_valid():
sc = Spectrascores(form).info()
return render(
request,'chat/view_cosine.html',{'form': form,'sc': sc}
)
else:
form = ViewCosineForm()
return render(request,{'form': form})
forms.py:
from .models import TestModel
class ViewCosineForm(forms.Form):
'''
Select any spectra from lab,library,Metadata
'''
sid = forms.ModelMultipleChoiceField(
queryset = TestModel.objects.all(),to_field_name = "test",required = False
)
从 shell 运行并向“/cosine/”打开一个 GET(??) 请求会产生这个:
$ ./manage.py runserver
[16/Feb/2021 23:32:08] "GET /cosine/ HTTP/1.1" 200 9083
[16/Feb/2021 23:32:08] "GET /static/js/jquery-3.3.1.min.js HTTP/1.1" 304 0
[16/Feb/2021 23:32:08] "GET /static/js/d3.min.js HTTP/1.1" 304 0
[16/Feb/2021 23:32:08] "GET /static/js/bootstrap.bundle.js HTTP/1.1" 304 0
Not Found: /favicon.ico
R[write to console]: Error: ignoring SIGPIPE signal
R[write to console]: Fatal error: unable to initialize the JIT
*** stack smashing detected ***: terminated
此时 Django ./manage.py runserver
退出。
然后,在这个错误发生后,跳回命令提示符并重新启动Django(./manage.py runserver
)导致另一个错误立即发生,使启动过程崩溃!此错误与每次(连续一次或多次)或根本不发生一样频繁:
(venv) [dave@localhost maldidb]$ ./manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/usr/lib64/python3.8/threading.py",line 932,in _bootstrap_inner
self.run()
File "/usr/lib64/python3.8/threading.py",line 870,in run
self._target(*self._args,**self._kwargs)
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/utils/autoreload.py",line 53,in wrapper
fn(*args,**kwargs)
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/core/management/commands/runserver.py",line 118,in inner_run
self.check(display_num_errors=True)
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/core/management/base.py",line 392,in check
all_issues = checks.run_checks(
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/core/checks/registry.py",line 70,in run_checks
new_errors = check(app_configs=app_configs,databases=databases)
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/core/checks/urls.py",line 13,in check_url_config
return check_resolver(resolver)
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/core/checks/urls.py",line 23,in check_resolver
return check_method()
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/urls/resolvers.py",line 408,in check
for pattern in self.url_patterns:
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/utils/functional.py",line 48,in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/urls/resolvers.py",line 589,in url_patterns
patterns = getattr(self.URLconf_module,"urlpatterns",self.URLconf_module)
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/utils/functional.py",line 582,in URLconf_module
return import_module(self.URLconf_name)
File "/home/dave/Dev/maldi-django/venv/lib64/python3.8/importlib/__init__.py",line 127,in import_module
return _bootstrap._gcd_import(name[level:],package,level)
File "<frozen importlib._bootstrap>",line 1014,in _gcd_import
File "<frozen importlib._bootstrap>",line 991,in _find_and_load
File "<frozen importlib._bootstrap>",line 975,in _find_and_load_unlocked
File "<frozen importlib._bootstrap>",line 671,in _load_unlocked
File "<frozen importlib._bootstrap_external>",line 783,in exec_module
File "<frozen importlib._bootstrap>",line 219,in _call_with_frames_removed
File "/home/dave/Dev/maldi-django/maldidb/soMedia/urls.py",line 9,in <module>
path('',include('chat.urls')),File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/django/urls/conf.py",line 34,in include
URLconf_module = import_module(URLconf_module)
File "/home/dave/Dev/maldi-django/venv/lib64/python3.8/importlib/__init__.py",in _call_with_frames_removed
File "/home/dave/Dev/maldi-django/maldidb/chat/urls.py",line 3,in <module>
from . import views
File "/home/dave/Dev/maldi-django/maldidb/chat/views.py",line 55,in <module>
from rpy2.robjects import r as R
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/rpy2/robjects/__init__.py",line 19,in <module>
from rpy2.robjects.robject import RObjectMixin,RObject
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/rpy2/robjects/robject.py",line 10,in <module>
rpy2.rinterface.initr_simple()
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/rpy2/rinterface.py",line 859,in initr_simple
_post_initr_setup()
File "/home/dave/Dev/maldi-django/venv/lib/python3.8/site-packages/rpy2/rinterface.py",line 940,in _post_initr_setup
signal.signal(signal.SIGINT,_sigint_handler)
File "/usr/lib64/python3.8/signal.py",line 47,in signal
handler = _signal.signal(_enum_to_int(signalnum),_enum_to_int(handler))
ValueError: signal only works in main thread
需要注意的是,views.py 中以以下装饰器 (@start_new_thread
) 的形式进行了线程处理,但在上述请求中没有进行线程处理。相反,线程是为某些其他视图保留的,但这些视图并未在上述上下文中执行。
同样,来自views.py:
def start_new_thread(function):
'''Starts a new thread for long-running tasks'''
def decorator(*args,**kwargs):
t = Thread(target = function,args = args,kwargs = kwargs)
t.daemon = True
t.start()
return t
return decorator
最后,信号?也许不相关,但在代码中有几个地方使用了信号,例如:
@receiver(post_save,sender=User)
def create_profile(sender,**kwargs):
...
然而,注释掉所有提到的接收器和导入到 Django 信号(例如 from django.db.models.signals import post_save
)仍然不会影响不稳定的行为。
编辑: 稍微简化了下,到rpy2的唯一引用就是这个的时候还是出现这个错误:
views.py
from rpy2.robjects import r as R
代码中没有其他对R或rpy2的引用,错误依旧!需要明确的是,删除这一行会导致上述错误停止。
可能是本地 R 环境出错?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)