diff --git a/sdk/eventhub/azure-eventhubs/azure/eventhub/_consumer_producer_mixin.py b/sdk/eventhub/azure-eventhubs/azure/eventhub/_consumer_producer_mixin.py index ef32cb7a591c..a14da749ee78 100644 --- a/sdk/eventhub/azure-eventhubs/azure/eventhub/_consumer_producer_mixin.py +++ b/sdk/eventhub/azure-eventhubs/azure/eventhub/_consumer_producer_mixin.py @@ -80,11 +80,10 @@ def _handle_exception(self, exception): return _handle_exception(exception, self) - def _do_retryable_operation(self, operation, timeout=None, **kwargs): + def _do_retryable_operation(self, operation, timeout=100000, **kwargs): # pylint:disable=protected-access - if not timeout: - timeout = 100000 # timeout equals to 0 means no timeout, set the value to be a large number. - timeout_time = time.time() + timeout + timeout_time = time.time() + ( + timeout if timeout else 100000) # timeout equals to 0 means no timeout, set the value to be a large number. retried_times = 0 last_exception = kwargs.pop('last_exception', None) operation_need_param = kwargs.pop('operation_need_param', True) @@ -100,7 +99,7 @@ def _do_retryable_operation(self, operation, timeout=None, **kwargs): timeout_time=timeout_time, entity_name=self._name) retried_times += 1 - log.info("%r has exhausted retry. Exception still occurs (%r)", self._name, last_exception) + log.info("%r operation has exhausted retry. Last exception: %r.", self._name, last_exception) raise last_exception def close(self, exception=None): diff --git a/sdk/eventhub/azure-eventhubs/azure/eventhub/aio/_consumer_producer_mixin_async.py b/sdk/eventhub/azure-eventhubs/azure/eventhub/aio/_consumer_producer_mixin_async.py index 33c944d41be4..444edd15a8a1 100644 --- a/sdk/eventhub/azure-eventhubs/azure/eventhub/aio/_consumer_producer_mixin_async.py +++ b/sdk/eventhub/azure-eventhubs/azure/eventhub/aio/_consumer_producer_mixin_async.py @@ -81,11 +81,10 @@ async def _handle_exception(self, exception): return await _handle_exception(exception, self) - async def _do_retryable_operation(self, operation, timeout=None, **kwargs): + async def _do_retryable_operation(self, operation, timeout=100000, **kwargs): # pylint:disable=protected-access - if not timeout: - timeout = 100000 # timeout equals to 0 means no timeout, set the value to be a large number. - timeout_time = time.time() + timeout + timeout_time = time.time() + ( + timeout if timeout else 100000) # timeout equals to 0 means no timeout, set the value to be a large number. retried_times = 0 last_exception = kwargs.pop('last_exception', None) operation_need_param = kwargs.pop('operation_need_param', True) @@ -101,7 +100,7 @@ async def _do_retryable_operation(self, operation, timeout=None, **kwargs): timeout_time=timeout_time, entity_name=self._name) retried_times += 1 - log.info("%r has exhausted retry. Exception still occurs (%r)", self._name, last_exception) + log.info("%r operation has exhausted retry. Last exception: %r.", self._name, last_exception) raise last_exception async def close(self, exception=None): diff --git a/sdk/eventhub/azure-eventhubs/examples/async_examples/send_async.py b/sdk/eventhub/azure-eventhubs/examples/async_examples/send_async.py index ac9ad098fee5..d24e73d0bc17 100644 --- a/sdk/eventhub/azure-eventhubs/examples/async_examples/send_async.py +++ b/sdk/eventhub/azure-eventhubs/examples/async_examples/send_async.py @@ -6,7 +6,7 @@ # -------------------------------------------------------------------------------------------- """ -An example to show sending events asynchronously to an Event Hub with partition keys. +An example to show sending individual events asynchronously to an Event Hub. """ # pylint: disable=C0111 @@ -45,6 +45,4 @@ async def send(producer, count): run(client)) start_time = time.time() loop.run_until_complete(tasks) -end_time = time.time() -run_time = end_time - start_time -print("Runtime: {} seconds".format(run_time)) +print("Runtime: {} seconds".format(time.time() - start_time)) diff --git a/sdk/eventhub/azure-eventhubs/examples/iothub_recv.py b/sdk/eventhub/azure-eventhubs/examples/iothub_recv.py index 0542f9c82b9c..ecc935669d13 100644 --- a/sdk/eventhub/azure-eventhubs/examples/iothub_recv.py +++ b/sdk/eventhub/azure-eventhubs/examples/iothub_recv.py @@ -17,9 +17,7 @@ client = EventHubClient.from_connection_string(iot_connection_str, network_tracing=False) consumer = client.create_consumer(consumer_group="$default", partition_id="0", event_position=EventPosition("-1"), operation='/messages/events') + with consumer: received = consumer.receive(timeout=5) print(received) - -eh_info = client.get_properties() -print(eh_info) diff --git a/sdk/eventhub/azure-eventhubs/examples/recv.py b/sdk/eventhub/azure-eventhubs/examples/recv.py index 68f0b5c214e8..11ed8747fc22 100644 --- a/sdk/eventhub/azure-eventhubs/examples/recv.py +++ b/sdk/eventhub/azure-eventhubs/examples/recv.py @@ -41,7 +41,4 @@ print(event_data.body_as_str()) total += 1 batch = consumer.receive(timeout=5) - - end_time = time.time() - run_time = end_time - start_time - print("Received {} messages in {} seconds".format(total, run_time)) + print("Received {} messages in {} seconds".format(total, time.time() - start_time)) diff --git a/sdk/eventhub/azure-eventhubs/examples/send.py b/sdk/eventhub/azure-eventhubs/examples/send.py index d559989c69a8..219d417447c1 100644 --- a/sdk/eventhub/azure-eventhubs/examples/send.py +++ b/sdk/eventhub/azure-eventhubs/examples/send.py @@ -6,9 +6,9 @@ # -------------------------------------------------------------------------------------------- """ -An example to show sending events to an Event Hub partition. -This is just an example of sending EventData, not performance optimal. -To have the best performance, send a batch EventData with one send() call. +An example to show sending individual events to an Event Hub partition. +Although this works, sending events in batches will get better performance. +See 'send_list_of_event_data.py' and 'send_event_data_batch.py' for an example of batching. """ # pylint: disable=C0111 @@ -26,10 +26,11 @@ client = EventHubClient(host=HOSTNAME, event_hub_path=EVENT_HUB, credential=EventHubSharedKeyCredential(USER, KEY), network_tracing=False) producer = client.create_producer(partition_id="0") + start_time = time.time() with producer: - # not performance optimal, but works. Please do send events in batch to get much better performance. for i in range(100): ed = EventData("msg") print("Sending message: {}".format(i)) - producer.send(ed) # please use batch_send for better performance. Refer to event_data_batch.py + producer.send(ed) +print("Send 100 messages in {} seconds".format(time.time() - start_time)) diff --git a/sdk/eventhub/azure-eventhubs/examples/send_event_data_batch.py b/sdk/eventhub/azure-eventhubs/examples/send_event_data_batch.py index 3ccaaf0a4fca..dfb7b8f3f749 100644 --- a/sdk/eventhub/azure-eventhubs/examples/send_event_data_batch.py +++ b/sdk/eventhub/azure-eventhubs/examples/send_event_data_batch.py @@ -42,6 +42,4 @@ def create_batch_data(producer): with producer: event_data_batch = create_batch_data(producer) producer.send(event_data_batch) -end_time = time.time() -run_time = end_time - start_time -print("Runtime: {} seconds".format(run_time)) +print("Runtime: {} seconds".format(time.time() - start_time)) diff --git a/sdk/eventhub/azure-eventhubs/examples/send_list_of_event_data.py b/sdk/eventhub/azure-eventhubs/examples/send_list_of_event_data.py index 74441d0f5bc3..715c220e6417 100644 --- a/sdk/eventhub/azure-eventhubs/examples/send_list_of_event_data.py +++ b/sdk/eventhub/azure-eventhubs/examples/send_list_of_event_data.py @@ -28,6 +28,7 @@ event_list = [] for i in range(1500): event_list.append(EventData('Hello World')) +start_time = time.time() with producer: - start_time = time.time() producer.send(event_list) +print("Runtime: {} seconds".format(time.time() - start_time))