在线程中创建的 SQLite 对象只能在 Django 2.2.2 和 ipdb 的同一线程中使用

Posted

技术标签:

【中文标题】在线程中创建的 SQLite 对象只能在 Django 2.2.2 和 ipdb 的同一线程中使用【英文标题】:SQLite objects created in a thread can only be used in that same thread with Django 2.2.2 and ipdb 【发布时间】:2019-10-21 15:43:38 【问题描述】:

我将我的 Django 项目更新到版本 2.2.2,现在当我调用 ipdb 进行调试时,服务器告诉我该错误。如果我回到 Django 2.2.1,错误就会消失。

System check identified no issues (0 silenced).
June 06, 2019 - 08:19:36
Django version 2.2.2, using settings 'laserapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
> /home/lynde/public_html/laserapp/gestion/views.py(192)get_context_data()
    191         import ipdb; ipdb.set_trace()
--> 192         context = super(ResumeOrderCodeView, self).get_context_data(**kwargs)
    193         try:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 95, in handle
    self.run(**options)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 585, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 570, in start_django
    reloader.run(django_main_thread)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 288, in run
    self.run_loop()
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 294, in run_loop
    next(ticker)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 334, in tick
    for filepath, mtime in self.snapshot_files():
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 350, in snapshot_files
    for file in self.watched_files():
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 249, in watched_files
    yield from iter_all_python_module_files()
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 103, in iter_all_python_module_files
    return iter_modules_and_files(modules, frozenset(_error_files))
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/django/utils/autoreload.py", line 120, in iter_modules_and_files
    sys_file_paths.append(module.__file__)
AttributeError: module '__main__' has no attribute '__file__'

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@python.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    %config Application.verbose_crash=True

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/IPython/core/history.py", line 780, in writeout_cache
    self._writeout_input_cache(conn)
  File "/home/lynde/public_html/env/laser/lib/python3.6/site-packages/IPython/core/history.py", line 764, in _writeout_input_cache
    (self.session_number,)+line)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 140030717908736 and this is thread id 140031011022656.

有人可以知道那里有什么问题吗?我在本地机器上使用 django runserver,带有 celery 和 postgresql 数据库。

谢谢!! :)

【问题讨论】:

【参考方案1】:

尝试使用--reload 标志来运行runserver。 ./manage.py runserver --noreload

它会停止服务器重新加载代码更改,但我认为可以避免您遇到的问题。

【讨论】:

行了!但是,为什么自动重载现在是 Django 2.2.2 中的一个问题?很奇怪.... 看起来是由这个补丁引起的:github.com/django/django/commit/… - 修复了这个问题code.djangoproject.com/ticket/30479 也为我工作。应该被接受的答案。 @LyNDe 为我工作,虽然不方便我必须重新启动我的应用程序。非常感谢!这应该被接受。

以上是关于在线程中创建的 SQLite 对象只能在 Django 2.2.2 和 ipdb 的同一线程中使用的主要内容,如果未能解决你的问题,请参考以下文章

在线程中创建的 SQLite 对象只能在 Django 2.2.2 和 ipdb 的同一线程中使用

与主线程在 QThread 中创建的连接对象失败,并且一个 QThread 中的对象之间的连接失败

我们如何在Android中重用已经在IOS中创建的Sqlite文件[重复]

在 RestKit 0.20.2 中创建的重复核心数据对象

将表插入到我使用 sqlite 在 python 中创建的数据库中时出错

无法打开在 Android 中创建的 SQLite DB(文件已加密或不是数据库)