<commit_before>import asyncio
import logging
import uuid

from zope.interface import implementer

import aioamqp
from aioamqp.exceptions import (AmqpClosedConnection,
                                ChannelClosed,
                                ConsumerCancelled)
from apium import registry
from apium.interfaces import IBroker, ISerializer

log = logging.getLogger(__name__)


@implementer(IBroker)
class Broker(object):
    """ IBroker implementation for AMQP """

    def __init__(self, driver):
        """ Build the broker for the given driver """
        self._driver = driver
        self._task_queue = asyncio.Queue(maxsize=1)
        self._serializer = registry.get(ISerializer)()
        self._start_consuming = False
        self._start_consuming_task = False
        self._start_consuming_result = False
        self._results = {}
        self._protocol = None
        self._channel = None
        self._consumer_tags = []
        self.connected = False

    @asyncio.coroutine
    def connect(self, url):
        """ Connect to the broker server.
        """
        try:
            self._protocol = yield from aioamqp.from_url(url)
            self._channel = yield from self._protocol.channel()
            self.connected = True
        except Exception:
            import traceback
            traceback.print_exc()

    @asyncio.coroutine
    def disconnect(self):
        """ Disconnect to the broker server. """
        self.connected = False
        log.info('basic cancel')
        for tag in self._consumer_tags:
            yield from self._channel.basic_cancel(tag)

        log.info('closing channel')
        yield from self._channel.close()
        self._channel = None
        self._protocol.stop()
        self._protocol = None

    @asyncio.coroutine
    def create_queue(self, queue):
        """ create a working queue where tasks will be pushed.<commit_msg>Use log.exception instead of traceback.print_exc<commit_after>import asyncio
import logging
import uuid

from zope.interface import implementer

import aioamqp
from aioamqp.exceptions import (AmqpClosedConnection,
                                ChannelClosed,
                                ConsumerCancelled)
from apium import registry
from apium.interfaces import IBroker, ISerializer

log = logging.getLogger(__name__)


@implementer(IBroker)
class Broker(object):
    """ IBroker implementation for AMQP """

    def __init__(self, driver):
        """ Build the broker for the given driver """
        self._driver = driver
        self._task_queue = asyncio.Queue(maxsize=1)
        self._serializer = registry.get(ISerializer)()
        self._start_consuming = False
        self._start_consuming_task = False
        self._start_consuming_result = False
        self._results = {}
        self._protocol = None
        self._channel = None
        self._consumer_tags = []
        self.connected = False

    @asyncio.coroutine
    def connect(self, url):
        """ Connect to the broker server.
        """
        try:
            self._protocol = yield from aioamqp.from_url(url)
            self._channel = yield from self._protocol.channel()
            self.connected = True
        except Exception:
            log.exception('Exception while connecting to broker')

    @asyncio.coroutine
    def disconnect(self):
        """ Disconnect to the broker server. """
        self.connected = False
        log.info('basic cancel')
        for tag in self._consumer_tags:
            yield from self._channel.basic_cancel(tag)

        log.info('closing channel')
        yield from self._channel.close()
        self._channel = None
        self._protocol.stop()
        self._protocol = None

    @asyncio.coroutine
    def create_queue(self, queue):
        """ create a working queue where tasks will be pushed.
{'fraction_non_alphanumeric': 0.08211297071129707, 'fraction_numerical': 0.0005230125523012552, 'mean_word_length': 2.25531914893617, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3352025', 'n_tokens_mistral': 1017, 'n_tokens_neox': 990, 'n_words': 271}