为什么我收到 pymongo.errors.AutoReconnect:连接池已暂停

问题描述 投票:0回答:4

我收到此自动重新连接错误,并且在调用

.objects
期间日志中大约有 100 个连接。这是文件:

class NotificationDoc(Document):
    patient_id = StringField(max_length=32)
    type = StringField(max_length=32)
    sms_sent = BooleanField(default=False)
    email_sent = BooleanField(default=False)
    sending_time = DateTimeField(default=datetime.utcnow)

    def __unicode__(self):
        return "{}_{}_{}".format(self.patient_id, self.type, self.sending_time.strftime('%Y-%m-%d'))

这是导致问题的查询调用:

    docs = NotificationDoc.objects(sending_time__lte=from_date)

这是完整的堆栈跟踪。我怎样才能理解发生了什么事?

pymongo.errors.AutoReconnect: connection pool paused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 451, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/celery/app/trace.py", line 734, in __protected_call__
    return self.run(*args, **kwargs)
  File "/app/src/reminder/configurations/app/worker.py", line 106, in schedule_recall
    schedule_recall_use_case.execute()
  File "/app/src/reminder/domain/notification/recall/use_cases/schedule_recall.py", line 24, in execute
    notifications_sent = self.notifications_provider.find_recalled_notifications_for_date(no_recalls_before_date)
  File "/app/src/reminder/data_providers/database/odm/repositories.py", line 42, in find_recalled_notifications_for_date
    if NotificationDoc.objects(sending_time__lte=from_date).count() > 0:
  File "/usr/local/lib/python3.8/site-packages/mongoengine/queryset/queryset.py", line 144, in count
    return super().count(with_limit_and_skip)
  File "/usr/local/lib/python3.8/site-packages/mongoengine/queryset/base.py", line 423, in count
    count = count_documents(
  File "/usr/local/lib/python3.8/site-packages/mongoengine/pymongo_support.py", line 38, in count_documents
    return collection.count_documents(filter=filter, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/pymongo/collection.py", line 1502, in count_documents
    return self.__database.client._retryable_read(
  File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1307, in _retryable_read
    with self._secondaryok_for_server(read_pref, server, session) as (
  File "/usr/local/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1162, in _secondaryok_for_server
    with self._get_socket(server, session) as sock_info:
  File "/usr/local/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.8/site-packages/pymongo/mongo_client.py", line 1099, in _get_socket
    with server.get_socket(
  File "/usr/local/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.8/site-packages/pymongo/pool.py", line 1371, in get_socket
    sock_info = self._get_socket(all_credentials)
  File "/usr/local/lib/python3.8/site-packages/pymongo/pool.py", line 1436, in _get_socket
    self._raise_if_not_ready(emit_event=True)
  File "/usr/local/lib/python3.8/site-packages/pymongo/pool.py", line 1407, in _raise_if_not_ready
    _raise_connection_failure(
  File "/usr/local/lib/python3.8/site-packages/pymongo/pool.py", line 250, in _raise_connection_failure
    raise AutoReconnect(msg) from error
pymongo.errors.AutoReconnect: mongo:27017: connection pool paused
python pymongo mongoengine
4个回答
3
投票

原来是芹菜使连接保持打开状态,所以这是解决方案

from mongoengine import disconnect
from celery.signals import task_prerun

@task_prerun.connect
def on_task_init(*args, **kwargs):
    disconnect(alias='default')
    connect(db, host=host, port=port, maxPoolSize=400, minPoolSize=200, alias='default')

3
投票

这可能与 PyMongo 不是分叉安全有关。如果您以任何方式使用进程,其中包括像uWSGI这样的服务器软件,甚至流行的ASGI服务器的一些配置

每次在 PyMongo 中运行查询时,该

MongoClient
对象就会变得不安全。从未运行过任何查询的
MongoClient
对象是分叉安全的。创建的
Database
Collection
对象将引用回其父对象
MongoClient
,而不检查是否存在。

我确实看到您正在使用 MongoEngine,它在底层使用 PyMongo。我不知道该特定库的语义,但我假设它们是相同的。

简而言之:作为分叉过程的一部分,您必须重新创建连接。


0
投票

pip install -U pymongo;已经修复;看到这个https://github.com/mongodb/mongo-python-driver/pull/944


0
投票

pymongo 4.1.1 -> 4.2.0 没有这个问题

© www.soinside.com 2019 - 2024. All rights reserved.