MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / cache

Function cache

python/paddle/reader/decorator.py:75–112  ·  view source on GitHub ↗

Cache the reader data into memory. Be careful that this method may take long time to process, and consume lots of memory. :code:`reader()` would only call once. Args: reader (generator): a reader object which yields data each time. Returns: gen

(reader: _Reader[_T])

Source from the content-addressed store, hash-verified

73
74
75def cache(reader: _Reader[_T]) -> _Reader[_T]:
76 """
77 Cache the reader data into memory.
78
79 Be careful that this method may take long time to process,
80 and consume lots of memory. :code:`reader()` would only
81 call once.
82
83 Args:
84 reader (generator): a reader object which yields
85 data each time.
86
87 Returns:
88 generator: a decorated reader object which yields data from cached memory.
89
90 Examples:
91 .. code-block:: pycon
92
93 >>> import paddle
94
95 >>> def reader():
96 ... for i in range(3):
97 ... yield i
98 >>> # All data is cached into memory
99 >>> cached_reader = paddle.base.io.cache(reader)
100
101 >>> for i in cached_reader():
102 ... print(i)
103 0
104 1
105 2
106 """
107 all_data = tuple(reader())
108
109 def __impl__() -> Generator[_T, None, None]:
110 yield from all_data
111
112 return __impl__
113
114
115# A temporary solution like builtin map function.

Callers 3

test_basicMethod · 0.85
test_int_collisionMethod · 0.85

Calls 1

readerFunction · 0.70

Tested by 3

test_basicMethod · 0.68
test_int_collisionMethod · 0.68