当前位置: 首页>>代码示例>>Python>>正文


Python base.Producer类代码示例

本文整理汇总了Python中kafka.producer.base.Producer的典型用法代码示例。如果您正苦于以下问题:Python Producer类的具体用法?Python Producer怎么用?Python Producer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Producer类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_switch_leader_async

    def test_switch_leader_async(self):
        topic = self.topic
        partition = 0

        # Test the base class Producer -- send_messages to a specific partition
        producer = Producer(self.client, async=True)

        # Send 10 random messages
        self._send_random_messages(producer, topic, partition, 10)

        # kill leader for partition
        self._kill_leader(topic, partition)

        logging.debug("attempting to send 'success' message after leader killed")

        # in async mode, this should return immediately
        producer.send_messages(topic, partition, 'success')

        # send to new leader
        self._send_random_messages(producer, topic, partition, 10)

        # wait until producer queue is empty
        while not producer.queue.empty():
            time.sleep(0.1)
        producer.stop()

        # count number of messages
        # Should be equal to 10 before + 1 recovery + 10 after
        self.assert_message_count(topic, 21, partitions=(partition,))
开发者ID:CrowdStrike,项目名称:kafka-python,代码行数:29,代码来源:test_failover_integration.py

示例2: __produce_kafka_message

def __produce_kafka_message(client, topic, key, event):
    partition_ids = client.get_partition_ids_for_topic(topic)
    key_stripped = key.decode("utf-8").upper().strip()
    key_hash = string_hashcode(key_stripped)
    partition_to_use = key_hash % len(partition_ids)
    logging.info("Using partition %s for key %s (stripped: %s, hash: %s)", partition_to_use, key, key_stripped, key_hash)

    producer = Producer(client)
    producer.send_messages(topic, partition_to_use, event)
开发者ID:AlexanderYastrebov,项目名称:nakadi,代码行数:9,代码来源:hack.py

示例3: test_acks_cluster_commit

    def test_acks_cluster_commit(self):
        partition = self.client.get_partition_ids_for_topic(self.topic)[0]
        start_offset = self.current_offset(self.topic, partition)

        producer = Producer(
            self.client,
            req_acks=Producer.ACK_AFTER_CLUSTER_COMMIT,
        )

        resp = producer.send_messages(self.topic, partition, self.msg("one"))
        self.assert_produce_response(resp, start_offset)
        self.assert_fetch_offset(partition, start_offset, [ self.msg("one") ])

        producer.stop()
开发者ID:Veterun,项目名称:KafkaPython,代码行数:14,代码来源:test_producer_integration.py

示例4: test_acks_local_write

    def test_acks_local_write(self):
        partition = self.client.get_partition_ids_for_topic(self.topic)[0]
        start_offset = self.current_offset(self.topic, partition)

        producer = Producer(
            self.client,
            req_acks=Producer.ACK_AFTER_LOCAL_WRITE,
        )
        resp = producer.send_messages(self.topic, partition, self.msg("one"))

        self.assert_produce_response(resp, start_offset)
        self.assert_fetch_offset(partition, start_offset, [ self.msg("one") ])

        producer.stop()
开发者ID:Veterun,项目名称:KafkaPython,代码行数:14,代码来源:test_producer_integration.py

示例5: test_producer_async_queue_overfilled

    def test_producer_async_queue_overfilled(self, mock):
        queue_size = 2
        producer = Producer(MagicMock(), async=True, async_queue_maxsize=queue_size)

        topic = b"test-topic"
        partition = 0
        message = b"test-message"

        with self.assertRaises(AsyncProducerQueueFull):
            message_list = [message] * (queue_size + 1)
            producer.send_messages(topic, partition, *message_list)
        self.assertEqual(producer.queue.qsize(), queue_size)
        for _ in xrange(producer.queue.qsize()):
            producer.queue.get()
开发者ID:gamechanger,项目名称:kafka-python,代码行数:14,代码来源:test_producer.py

示例6: producer

def producer():
    client = KafkaClient(hosts=['127.0.0.1:9092', ])
    producer = Producer(client)
    # bad_data_types = (u'你怎么样?', 12, ['a', 'list'],
    # ('a', 'tuple'), {'a': 'dict'}, None,)
    # for m in bad_data_types:
    #     producer.send_messages(topic, partition, m)

    good_data_types = (b'a string!',)
    for m in good_data_types:
        # This should not raise an exception
        responses = producer.send_messages(topic, partition, m) #写数据可以指定不同的partition,然后启动不同的comsumer来消费知道的partition的数据

    for i in six.moves.xrange(10000):
        responses = producer.send_messages(topic, partition, 'msg_%d' % i)
开发者ID:openslack,项目名称:openslack-web,代码行数:15,代码来源:kafka_product.py

示例7: test_acks_none

    def test_acks_none(self):
        partition = self.client.get_partition_ids_for_topic(self.topic)[0]
        start_offset = self.current_offset(self.topic, partition)

        producer = Producer(
            self.client,
            req_acks=Producer.ACK_NOT_REQUIRED,
        )
        resp = producer.send_messages(self.topic, partition, self.msg("one"))

        # No response from produce request with no acks required
        self.assertEqual(len(resp), 0)

        # But the message should still have been delivered
        self.assert_fetch_offset(partition, start_offset, [ self.msg("one") ])
        producer.stop()
开发者ID:Veterun,项目名称:KafkaPython,代码行数:16,代码来源:test_producer_integration.py

示例8: test_producer_message_types

    def test_producer_message_types(self):

        producer = Producer(MagicMock())
        topic = b"test-topic"
        partition = 0

        bad_data_types = (u'你怎么样?', 12, ['a', 'list'], ('a', 'tuple'), {'a': 'dict'})
        for m in bad_data_types:
            with self.assertRaises(TypeError):
                logging.debug("attempting to send message of type %s", type(m))
                producer.send_messages(topic, partition, m)

        good_data_types = (b'a string!',)
        for m in good_data_types:
            # This should not raise an exception
            producer.send_messages(topic, partition, m)
开发者ID:MiguelPeralvo,项目名称:kafka-python,代码行数:16,代码来源:test_producer.py

示例9: test_producer_message_types

    def test_producer_message_types(self):

        producer = Producer(MagicMock())
        topic = b"test-topic"
        partition = 0

        bad_data_types = (u"你怎么样?", 12, ["a", "list"], ("a", "tuple"), {"a": "dict"}, None)
        for m in bad_data_types:
            with self.assertRaises(TypeError):
                logging.debug("attempting to send message of type %s", type(m))
                producer.send_messages(topic, partition, m)

        good_data_types = (b"a string!",)
        for m in good_data_types:
            # This should not raise an exception
            producer.send_messages(topic, partition, m)
开发者ID:gamechanger,项目名称:kafka-python,代码行数:16,代码来源:test_producer.py

示例10: test_switch_leader

    def test_switch_leader(self):
        topic = self.topic
        partition = 0

        # Testing the base Producer class here so that we can easily send
        # messages to a specific partition, kill the leader for that partition
        # and check that after another broker takes leadership the producer
        # is able to resume sending messages

        # require that the server commit messages to all in-sync replicas
        # so that failover doesn't lose any messages on server-side
        # and we can assert that server-side message count equals client-side
        producer = Producer(self.client, async=False,
                            req_acks=Producer.ACK_AFTER_CLUSTER_COMMIT)

        # Send 100 random messages to a specific partition
        self._send_random_messages(producer, topic, partition, 100)

        # kill leader for partition
        self._kill_leader(topic, partition)

        # expect failure, but don't wait more than 60 secs to recover
        recovered = False
        started = time.time()
        timeout = 60
        while not recovered and (time.time() - started) < timeout:
            try:
                log.debug("attempting to send 'success' message after leader killed")
                producer.send_messages(topic, partition, b'success')
                log.debug("success!")
                recovered = True
            except (FailedPayloadsError, ConnectionError, RequestTimedOutError,
                    NotLeaderForPartitionError):
                log.debug("caught exception sending message -- will retry")
                continue

        # Verify we successfully sent the message
        self.assertTrue(recovered)

        # send some more messages to new leader
        self._send_random_messages(producer, topic, partition, 100)

        # count number of messages
        # Should be equal to 100 before + 1 recovery + 100 after
        # at_least=True because exactly once delivery isn't really a thing
        self.assert_message_count(topic, 201, partitions=(partition,),
                                  at_least=True)
开发者ID:jianbin-wei,项目名称:kafka-python,代码行数:47,代码来源:test_failover_integration.py

示例11: test_switch_leader

    def test_switch_leader(self):
        topic = self.topic
        partition = 0

        # Test the base class Producer -- send_messages to a specific partition
        producer = Producer(self.client, async=False,
                            req_acks=Producer.ACK_AFTER_CLUSTER_COMMIT)

        # Send 10 random messages
        self._send_random_messages(producer, topic, partition, 100)

        # kill leader for partition
        self._kill_leader(topic, partition)

        # expect failure, but dont wait more than 60 secs to recover
        recovered = False
        started = time.time()
        timeout = 60
        while not recovered and (time.time() - started) < timeout:
            try:
                logging.debug("attempting to send 'success' message after leader killed")
                producer.send_messages(topic, partition, b'success')
                logging.debug("success!")
                recovered = True
            except (FailedPayloadsError, ConnectionError):
                logging.debug("caught exception sending message -- will retry")
                continue

        # Verify we successfully sent the message
        self.assertTrue(recovered)

        # send some more messages to new leader
        self._send_random_messages(producer, topic, partition, 100)

        # count number of messages
        # Should be equal to 10 before + 1 recovery + 10 after
        self.assert_message_count(topic, 201, partitions=(partition,))
开发者ID:CrowdStrike,项目名称:kafka-python,代码行数:37,代码来源:test_failover_integration.py

示例12: test_switch_leader_async

    def test_switch_leader_async(self):
        topic = self.topic
        partition = 0

        # Test the base class Producer -- send_messages to a specific partition
        producer = Producer(self.client, async=True,
                            batch_send_every_n=15,
                            batch_send_every_t=3,
                            req_acks=Producer.ACK_AFTER_CLUSTER_COMMIT,
                            async_log_messages_on_error=False)

        # Send 10 random messages
        self._send_random_messages(producer, topic, partition, 10)
        self._send_random_messages(producer, topic, partition + 1, 10)

        # kill leader for partition
        self._kill_leader(topic, partition)

        log.debug("attempting to send 'success' message after leader killed")

        # in async mode, this should return immediately
        producer.send_messages(topic, partition, b'success')
        producer.send_messages(topic, partition + 1, b'success')

        # send to new leader
        self._send_random_messages(producer, topic, partition, 10)
        self._send_random_messages(producer, topic, partition + 1, 10)

        # Stop the producer and wait for it to shutdown
        producer.stop()
        started = time.time()
        timeout = 60
        while (time.time() - started) < timeout:
            if not producer.thread.is_alive():
                break
            time.sleep(0.1)
        else:
            self.fail('timeout waiting for producer queue to empty')

        # count number of messages
        # Should be equal to 10 before + 1 recovery + 10 after
        # at_least=True because exactly once delivery isn't really a thing
        self.assert_message_count(topic, 21, partitions=(partition,),
                                  at_least=True)
        self.assert_message_count(topic, 21, partitions=(partition + 1,),
                                  at_least=True)
开发者ID:jianbin-wei,项目名称:kafka-python,代码行数:46,代码来源:test_failover_integration.py

示例13: test_cleanup_is_called_on_running_producer

 def test_cleanup_is_called_on_running_producer(self):
     producer = Producer(MagicMock(), async_send=True)
     producer.stopped = False
     with patch.object(producer, 'stop') as mocked_stop:
         producer._cleanup_func(producer)
         self.assertEqual(mocked_stop.call_count, 1)
开发者ID:dpkp,项目名称:kafka-python,代码行数:6,代码来源:test_producer_legacy.py

示例14: test_cleanup_is_not_called_on_stopped_producer

 def test_cleanup_is_not_called_on_stopped_producer(self):
     producer = Producer(MagicMock(), async=True)
     producer.stopped = True
     with patch.object(producer, "stop") as mocked_stop:
         producer._cleanup_func(producer)
         self.assertEqual(mocked_stop.call_count, 0)
开发者ID:gamechanger,项目名称:kafka-python,代码行数:6,代码来源:test_producer.py


注:本文中的kafka.producer.base.Producer类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。