为什么我会收到pymongo.errors.AutoReconnect: connection pool paused错误提示

3

我遇到了"autoreconnect"的错误,在这次对.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
3个回答

3
这可能与PyMongo不支持进程分叉(fork-safe)有关。如果您以任何方式使用进程池(process pool),包括服务器软件如uWSGI和一些流行的ASGI服务器配置, 都会受到影响。
每次在PyMongo中运行查询时,MongoClient对象都会变得不支持进程分叉(fork-unsafe)。从未运行任何查询的MongoClient对象是支持进程分叉(fork-safe)的。创建的DatabaseCollection对象将引用其父MongoClient而无需检查其是否存在。
我看到你正在使用MongoEngine,它在底层使用PyMongo。我不知道这个特定库的语义,但我认为它们是相同的。
简而言之:作为forking过程的一部分,你必须重新创建你的连接。

3

原来是Celery在保持连接,这是解决方案

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')

链接=404,请解释您的问题... - Omar

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接