【问题标题】:Using gevent in custom Django management command在自定义 Django 管理命令中使用 gevent
【发布时间】:2018-06-19 04:19:04
【问题描述】:

我正在尝试使用 gevent 和 Django 管理命令来打个招呼。

from gevent import monkey
monkey.patch_all()

from django.core.management.base import BaseCommand


class Command(BaseCommand):
    # https://docs.djangoproject.com/en/1.11/howto/custom-management-commands/

    def add_arguments(self, parser):
        parser.add_argument(
            '--since',
            dest='since',
            type=int
        )

    def handle(self, *args, **options):
        self.stdout.write(str(options['since']))

没有尝试使用 gevent 或访问数据库,这给了我:

Traceback (most recent call last):
  File "./manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "env/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "env/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "env/lib/python3.6/site-packages/django/core/management/base.py", line 296, in run_from_argv
    connections.close_all()
  File "env/lib/python3.6/site-packages/django/db/utils.py", line 234, in close_all
    connection.close()
  File "env/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 221, in close
    self.validate_thread_sharing()
  File "env/lib/python3.6/site-packages/django/db/backends/base/base.py", line 542, in validate_thread_sharing
    % (self.alias, self._thread_ident, thread.get_ident())
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 140735610057536 and this is thread id 4446749960.

这是 Django 1.11.4 和 gevent 1.2.2。

【问题讨论】:

    标签: python django sqlite gevent


    【解决方案1】:

    在你的 manage.py 脚本中调用patch_all,你这个菜鸟。

    【讨论】:

    • 退出编辑以删除you noob。我是提出并回答问题的人。这让它变得有趣,而不是侮辱。哈哈。是的。
    猜你喜欢
    • 2016-03-28
    • 2016-07-09
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2012-03-26
    • 2021-03-09
    相关资源
    最近更新 更多