gpt4 book ai didi

python - 在自定义 Django 管理命令中使用 gevent

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:09 25 4
gpt4 key购买 nike

我正在尝试使用 gevent 和 Django 管理命令创建一个 hello world。

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。

最佳答案

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

关于python - 在自定义 Django 管理命令中使用 gevent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48179909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com