Skip to content

Commit e09df34

Browse files
committed
Adds description to Object Pool
1 parent 74c4935 commit e09df34

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

creational/pool.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,33 @@
22
# -*- coding: utf-8 -*-
33

44
"""
5+
*What is this pattern about?
6+
This pattern is used when creating an object is costly (and they are
7+
created frequently) but only a few are used at a time. With a Pool we
8+
can manage those instances we have as of now by caching them. Now it
9+
is possible to skip the costly creation of an object if one is
10+
available in the pool.
11+
A pool allows to 'check out' an inactive object and then to return it.
12+
If none are available the pool creates one to provide without wait.
13+
14+
*What does this example do?
15+
In this example queue.Queue is used to create the pool (wrapped in a
16+
custom ObjectPool object to use with the with statement), and it is
17+
populated with strings.
18+
As we can see, the first string object put in "yam" is USED by the
19+
with statement. But because it is released back into the pool
20+
aftwerwards it is reused by the explicit call to sample_queue.get().
21+
Same thing happens with "sam", when the ObjectPool created insided the
22+
function is deleted (by the GC) and the object is returned.
23+
24+
*Where is the pattern used practically?
25+
26+
*References:
527
http://stackoverflow.com/questions/1514120/python-implementation-of-the-object-pool-design-pattern
28+
https://sourcemaking.com/design_patterns/object_pool
629
"""
730

831

9-
1032
class ObjectPool(object):
1133

1234
def __init__(self, queue, auto_get=False):

0 commit comments

Comments
 (0)