File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change 2
2
# -*- coding: utf-8 -*-
3
3
4
4
"""
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:
5
27
http://stackoverflow.com/questions/1514120/python-implementation-of-the-object-pool-design-pattern
28
+ https://sourcemaking.com/design_patterns/object_pool
6
29
"""
7
30
8
31
9
-
10
32
class ObjectPool (object ):
11
33
12
34
def __init__ (self , queue , auto_get = False ):
You can’t perform that action at this time.
0 commit comments