def publish(self, *args, **kwargs):
        '''
        Publish a message. Caller can supply an optional callback which will
        be fired when the transaction is committed. Tries very hard to avoid
        closed and inactive channels, but a ChannelError or ConnectionError
        may still be raised.
        '''
        user_cb = kwargs.pop('cb', None)

        # If the first channel we grab is inactive, continue fetching until
        # we get an active channel, then put the inactive channels back in
        # the pool. Try to keep the overhead to a minimum.
        channel = self._get_channel()

        if channel and not channel.active:
            inactive_channels = set()
            while channel and not channel.active:
                inactive_channels.add(channel)
                channel = self._get_channel()
            self._free_channels.update(inactive_channels)

        # When the transaction is committed, add the channel back to the pool
        # and call any user-defined callbacks. If there is anything in queue,
        # pop it and call back to publish(). Only do so if the channel is
        # still active though, because otherwise the message will end up at
        # the back of the queue, breaking the original order.
        def committed():
            self._free_channels.add(channel)
            if channel.active and not channel.closed:
                self._process_queue()
            if user_cb is not None:
                user_cb()

        if channel:
            channel.publish_synchronous(*args, cb=committed, **kwargs)
        else:
            kwargs['cb'] = user_cb
            self._queue.append((args, kwargs))
Publish a message. Caller can supply an optional callback which will
        be fired when the transaction is committed. Tries very hard to avoid
        closed and inactive channels, but a ChannelError or ConnectionError
        may still be raised.