1# Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Experimental API for controlling threading in `tf.data` pipelines.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20from tensorflow.python.data.ops import dataset_ops 21from tensorflow.python.ops import gen_experimental_dataset_ops 22 23 24class _SleepDataset(dataset_ops.UnaryUnchangedStructureDataset): 25 """A `Dataset` that sleeps before producing each upstream element.""" 26 27 def __init__(self, input_dataset, sleep_microseconds): 28 self._input_dataset = input_dataset 29 self._sleep_microseconds = sleep_microseconds 30 variant_tensor = gen_experimental_dataset_ops.experimental_sleep_dataset( 31 self._input_dataset._variant_tensor, # pylint: disable=protected-access 32 self._sleep_microseconds, 33 **dataset_ops.flat_structure(self)) 34 super(_SleepDataset, self).__init__(input_dataset, variant_tensor) 35 36 37def sleep(sleep_microseconds): 38 """Sleeps for `sleep_microseconds` before producing each input element. 39 40 Args: 41 sleep_microseconds: The number of microseconds to sleep before producing an 42 input element. 43 44 Returns: 45 A `Dataset` transformation function, which can be passed to 46 `tf.data.Dataset.apply`. 47 """ 48 49 def _apply_fn(dataset): 50 return _SleepDataset(dataset, sleep_microseconds) 51 52 return _apply_fn 53