本文整理汇总了Python中django.db.close_old_connections方法的典型用法代码示例。如果您正苦于以下问题:Python db.close_old_connections方法的具体用法?Python db.close_old_connections怎么用?Python db.close_old_connections使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db
的用法示例。
在下文中一共展示了db.close_old_connections方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: check_password
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def check_password(environ, username, password):
"""
Authenticates against Django's auth database
mod_wsgi docs specify None, True, False as return value depending
on whether the user exists and authenticates.
"""
UserModel = auth.get_user_model()
# db connection state is managed similarly to the wsgi handler
# as mod_wsgi may call these functions outside of a request/response cycle
db.reset_queries()
try:
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return None
if not user.is_active:
return None
return user.check_password(password)
finally:
db.close_old_connections()
示例2: groups_for_user
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def groups_for_user(environ, username):
"""
Authorizes a user based on groups
"""
UserModel = auth.get_user_model()
db.reset_queries()
try:
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return []
if not user.is_active:
return []
return [force_bytes(group.name) for group in user.groups.all()]
finally:
db.close_old_connections()
示例3: check_password
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def check_password(environ, username, password):
"""
Authenticate against Django's auth database.
mod_wsgi docs specify None, True, False as return value depending
on whether the user exists and authenticates.
"""
# db connection state is managed similarly to the wsgi handler
# as mod_wsgi may call these functions outside of a request/response cycle
db.reset_queries()
try:
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return None
if not user.is_active:
return None
return user.check_password(password)
finally:
db.close_old_connections()
示例4: process_task
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def process_task(self, body, message):
logger.info('Processing message: %r', message)
try:
self._handle_task(body, message)
except (OperationalError, InterfaceError):
# Lost DB connection, close DB and don't ack() msg.
# A new DB connection will be re-opened next time we
# try to access the DB. Msg will be re-processed
# after SQS visibility timeout passes.
logger.exception("DB connection lost. Cleaning up connections")
return close_old_connections()
except: # NOQA
logger.exception("Failed to process message: %r", message)
logger.info("ACKing message %r", message)
if self.connection.as_uri().lower().startswith('sqs://'):
# HACK: Can't seem to get message.ack() to work for SQS
# backend. Without this hack, messages will keep
# re-appearing after the visibility_timeout expires.
# See https://github.com/celery/kombu/issues/758
return self._sqs_ack(message)
return message.ack()
示例5: check_password
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def check_password(environ, username, password):
"""
Authenticates against Django's auth database
mod_wsgi docs specify None, True, False as return value depending
on whether the user exists and authenticates.
"""
# db connection state is managed similarly to the wsgi handler
# as mod_wsgi may call these functions outside of a request/response cycle
db.reset_queries()
try:
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return None
if not user.is_active:
return None
return user.check_password(password)
finally:
db.close_old_connections()
示例6: groups_for_user
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def groups_for_user(environ, username):
"""
Authorizes a user based on groups
"""
db.reset_queries()
try:
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return []
if not user.is_active:
return []
return [force_bytes(group.name) for group in user.groups.all()]
finally:
db.close_old_connections()
示例7: closing_iterator_wrapper
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def closing_iterator_wrapper(iterable, close):
try:
for item in iterable:
yield item
finally:
request_finished.disconnect(close_old_connections)
close() # will fire request_finished
request_finished.connect(close_old_connections)
示例8: __call__
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def __call__(self, environ):
# Set up middleware if needed. We couldn't do this earlier, because
# settings weren't available.
if self._request_middleware is None:
self.load_middleware()
request_started.disconnect(close_old_connections)
request_started.send(sender=self.__class__, environ=environ)
request_started.connect(close_old_connections)
request = WSGIRequest(environ)
# sneaky little hack so that we can easily get round
# CsrfViewMiddleware. This makes life easier, and is probably
# required for backwards compatibility with external tests against
# admin views.
request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
# Request goes through middleware.
response = self.get_response(request)
# Attach the originating request to the response so that it could be
# later retrieved.
response.wsgi_request = request
# We're emulating a WSGI server; we must call the close method
# on completion.
if response.streaming:
response.streaming_content = closing_iterator_wrapper(
response.streaming_content, response.close)
else:
request_finished.disconnect(close_old_connections)
response.close() # will fire request_finished
request_finished.connect(close_old_connections)
return response
示例9: closing_iterator_wrapper
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def closing_iterator_wrapper(iterable, close):
try:
yield from iterable
finally:
request_finished.disconnect(close_old_connections)
close() # will fire request_finished
request_finished.connect(close_old_connections)
示例10: __call__
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def __call__(self, environ):
# Set up middleware if needed. We couldn't do this earlier, because
# settings weren't available.
if self._middleware_chain is None:
self.load_middleware()
request_started.disconnect(close_old_connections)
request_started.send(sender=self.__class__, environ=environ)
request_started.connect(close_old_connections)
request = WSGIRequest(environ)
# sneaky little hack so that we can easily get round
# CsrfViewMiddleware. This makes life easier, and is probably
# required for backwards compatibility with external tests against
# admin views.
request._dont_enforce_csrf_checks = not self.enforce_csrf_checks
# Request goes through middleware.
response = self.get_response(request)
# Simulate behaviors of most Web servers.
conditional_content_removal(request, response)
# Attach the originating request to the response so that it could be
# later retrieved.
response.wsgi_request = request
# Emulate a WSGI server by calling the close method on completion.
if response.streaming:
response.streaming_content = closing_iterator_wrapper(
response.streaming_content, response.close)
else:
request_finished.disconnect(close_old_connections)
response.close() # will fire request_finished
request_finished.connect(close_old_connections)
return response
示例11: groups_for_user
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def groups_for_user(environ, username):
"""
Authorize a user based on groups
"""
db.reset_queries()
try:
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
return []
if not user.is_active:
return []
return [force_bytes(group.name) for group in user.groups.all()]
finally:
db.close_old_connections()
示例12: close_old_django_connections
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def close_old_django_connections():
"""
Close django connections unless running with sync=True.
"""
if Conf.SYNC:
logger.warning(
"Preserving django database connections because sync=True. Beware "
"that tasks are now injected in the calling context/transactions "
"which may result in unexpected bahaviour."
)
else:
db.close_old_connections()
示例13: get_connection
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def get_connection(list_key: str = Conf.PREFIX):
if transaction.get_autocommit(
using=Conf.ORM
): # Only True when not in an atomic block
# Make sure stale connections in the broker thread are explicitly
# closed before attempting DB access.
# logger.debug("Broker thread calling close_old_connections")
db.close_old_connections()
else:
logger.debug("Broker in an atomic transaction")
return OrmQ.objects.using(Conf.ORM)
示例14: check_db_connection
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def check_db_connection(function):
""" Check that the database connection is not in "already closed" state.
This tends to happen when postgres is restarted. Connections will persist
in this state throwing "connection already closed" errors until the
old connections are disposed of.
"""
def wrapper(self, *args, **kwargs):
if not disable_check_db_connection_decorator:
try:
db.connection.cursor()
except Exception as e:
if "connection already closed" in str(e):
log.warning("check_db_connection: connection already closed")
try:
db.close_old_connections()
log.info("check_db_connection: removed old connections")
except Exception as e:
log.exception("check_db_connection: we failed to fix the failure", e=e)
raise
else:
raise
result = function(self, *args, **kwargs)
return result
return wrapper
示例15: setUp
# 需要导入模块: from django import db [as 别名]
# 或者: from django.db import close_old_connections [as 别名]
def setUp(self) -> None:
super().setUp()
signals.request_started.disconnect(close_old_connections)
signals.request_finished.disconnect(close_old_connections)
self.session_cookie: Optional[Dict[str, str]] = None