Connection and Producer Pools

官方文档只能说写的很 low

让我们自己从代码中发现原理吧

from kombu.pools import connections
from kombu import Connection

uri = 'amqp://guest:guest@192.168.178.100:5672//'

connection = Connection(uri)

with connections[connection].acquire(block=True) as conn:
    assert conn == connection

首先这玩意不是一个保险柜

site-packages/kombu/pools.py

class Connections(PoolGroup):
    """Collection of connection pools."""

    def create(self, connection, limit):
        return connection.Pool(limit=limit)


connections = register_group(Connections(limit=use_global_limit))

site-packages/kombu/pools.py

def register_group(group):
    """Register group (can be used as decorator)."""
    _groups.append(group)
    return group

site-packages/kombu/pools.py

class PoolGroup(EqualityDict):
    """Collection of resource pools."""

    def __init__(self, limit=None, close_after_fork=True):
        self.limit = limit
        self.close_after_fork = close_after_fork
        if self.close_after_fork and register_after_fork is not None:
            register_after_fork(self, _after_fork_cleanup_group)

    def create(self, resource, limit):
        raise NotImplementedError('PoolGroups must define ``create``')

    def __missing__(self, resource):
        limit = self.limit
        if limit is use_global_limit:
            limit = get_limit()
        k = self[resource] = self.create(resource, limit)
        return k

site-packages/kombu/utils/collections.py

class EqualityDict(dict):
    """Dict using the eq operator for keying."""

    def __getitem__(self, key):
        h = eqhash(key)
        if h not in self:
            return self.__missing__(key)
        return super().__getitem__(h)

    def __setitem__(self, key, value):
        return super().__setitem__(eqhash(key), value)

    def __delitem__(self, key):
        return super().__delitem__(eqhash(key))

universe_king
3.4k 声望678 粉丝