我不是要尝试编写任何进程,但是在我的Django站点上一直遇到这个错误.我看到其他一些人问这个问题,但他们都试图使用多个线程.
这是在Django 1.11,Python 3.6,Matplotlib 2.0.0上.我也会注意到这个问题发生在我的Mac上,但不在我的现场Heroku服务器上.
我正在使用Pyplot来创建用户制作的模型实例的一些可视化,并且在我尝试运行它时大约有一半的时间:
异常类型:RuntimeError
异常值:主线程不在主循环中
我运行的代码的最后一行只是plt.figure()调用
跟踪:
File "/Users/Mark/Desktop/Professional/FSC/water/WATER/hydrograph/views.py" in processData
397. hydroFigure = plt.figure()
File "/Users/Mark/Desktop/Professional/FSC/water/WATER/ENV/lib/python3.6/site- packages/matplotlib/pyplot.py" in figure
535. **kwargs)
File "/Users/Mark/Desktop/Professional/FSC/water/WATER/ENV/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py" in new_figure_manager
81. return new_figure_manager_given_figure(num, figure)
File "/Users/Mark/Desktop/Professional/FSC/water/WATER/ENV/lib/python3.6/site-packages/matplotlib/backends/backend_tkagg.py" in new_figure_manager_given_figure
98. icon_img = Tk.PhotoImage(file=icon_fname)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py" in __init__
3539. Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py" in __init__
3495. self.tk.call(('image', 'create', imgtype, name,) + options)
Exception Type: RuntimeError at /hydrograph/
Exception Value: main thread is not in main loop
Run Code Online (Sandbox Code Playgroud)
关于我能做什么的任何想法?
我试图捕获一个DidNotExist异常,并且很难检查抛出该异常的代码。我无法到达将其抛出到pdb中的那一行,并且在调试跟踪中,没有关于引发异常的对象的信息,因为它包含在实例变量下,因此django跟踪不会显示其值。
我已经阅读了https://docs.djangoproject.com/zh-CN/1.11/ref/exceptions/#objectdoesnotexist上的文档,并在“ 捕获任何DidNotExist错误”中发现了这个问题。这两个都表明,要捕获任何DidNotExist异常,只需导入ObjectDoesNotExist即可覆盖所有异常。在我来说不是。我的除ObjectDoesNotExist块外未捕获到异常。
码:
from django.core.exceptions import ObjectDoesNotExist
def check_permission(user):
"""function checks whether the user is in the list of allowed groups"""
for option in ALLOWED: # list of groups, constant
try:
if user.groups.get().name == option:
return True
except ObjectDoesNotExist:
pass
else:
pass
return False
Run Code Online (Sandbox Code Playgroud)
跟踪显示为:
File
".../lib/python3.6/site-packages/django/db/models/query.py", line 379, in get self.model._meta.object_name
django.contrib.auth.models.DoesNotExist: Group matching query does not exist.
Run Code Online (Sandbox Code Playgroud)
这是从我的代码行中抛出的:
if user.groups.get().name == option
Run Code Online (Sandbox Code Playgroud)
尝试访问django.contrib.auth.models.DoesNotExist只会返回一个
AttributeError: model 'django.contrib.auth.models' has no attribute 'DoesNotExist'
Run Code Online (Sandbox Code Playgroud)
我该如何捕捉?有时用户将没有任何组,即使该站点是为永不中断而设计的,但仍令我感到不安的是,这里存在易破解的代码。
编辑:python 3.6.2 Django …