Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Jim Baker - A Winning Strategy with The Weakes...

Jim Baker - A Winning Strategy with The Weakest Link: how to use weak references to make your code more robust

Working with weak references should not just be for Python wizards. Whether you have a cache, memoizing a function, tracking objects, or various other bookkeeping needs, you definitely do not want code leaking memory or resources. In this talk, we will look at illuminating examples drawn from a variety of sources on how to use weak references to prevent such bugs.

https://us.pycon.org/2015/schedule/presentation/468/

PyCon 2015

April 18, 2015
Tweet

More Decks by PyCon 2015

Other Decks in Programming

Transcript

  1. Write More Robust Code with Weak References Jim Baker Write

    More Robust Code with Weak References Jim Baker jim.baker@{python.org, rackspace.com}
  2. Write More Robust Code with Weak References Jim Baker Some

    possible questions Questions you might have in coming to this talk: What exactly are weak references?
  3. Write More Robust Code with Weak References Jim Baker Some

    possible questions Questions you might have in coming to this talk: What exactly are weak references? How do they differ from strong references?
  4. Write More Robust Code with Weak References Jim Baker Some

    possible questions Questions you might have in coming to this talk: What exactly are weak references? How do they differ from strong references? When would I use them anyway?
  5. Write More Robust Code with Weak References Jim Baker About

    me Core developer of Jython Co-author of Definitive Guide to Jython from Apress
  6. Write More Robust Code with Weak References Jim Baker About

    me Core developer of Jython Co-author of Definitive Guide to Jython from Apress Software developer at Rackspace
  7. Write More Robust Code with Weak References Jim Baker About

    me Core developer of Jython Co-author of Definitive Guide to Jython from Apress Software developer at Rackspace Lecturer in CS at Univ of Colorado at Boulder
  8. Write More Robust Code with Weak References Jim Baker Defining

    a weak reference A weak reference to an object is not enough to keep the object alive: when the only remaining references to a referent are weak references, garbage collection is free to destroy the referent and reuse its memory for something else. However, until the object is actually destroyed the weak reference may return the object even if there are no strong references to it. (https://docs.python.org/3/library/weakref.html)
  9. Write More Robust Code with Weak References Jim Baker Weak

    references Initially proposed in PEP 205
  10. Write More Robust Code with Weak References Jim Baker Weak

    references Initially proposed in PEP 205 Implemented in Python 2.1 (released April 2001)
  11. Write More Robust Code with Weak References Jim Baker Weak

    references Initially proposed in PEP 205 Implemented in Python 2.1 (released April 2001) Released 14 years ago!
  12. Write More Robust Code with Weak References Jim Baker Example:

    WeakSet First, let’s import WeakSet. Many uses of weak references are with respect to the collections provided by the weakref module: from weakref import WeakSet
  13. Write More Robust Code with Weak References Jim Baker Weak

    referenceable classes Define a class X like so: class X(object): pass NB: str and certain other classes are not weak referenceable in CPython, but their subclasses can be
  14. Write More Robust Code with Weak References Jim Baker Construction

    Construct a weak set and add an element to it. We then list the set: s = WeakSet() s.add(X()) list(s)
  15. Write More Robust Code with Weak References Jim Baker Conclusions

    s is (eventually) empty - with list(s), we get []
  16. Write More Robust Code with Weak References Jim Baker Conclusions

    s is (eventually) empty - with list(s), we get [] May require a round of garbage collection with gc.collect()
  17. Write More Robust Code with Weak References Jim Baker Some

    possible questions Questions you might have in coming to this talk: What exactly are weak references? How do they differ from strong references? When would I use them anyway? To prevent memory and resource leaks.
  18. Write More Robust Code with Weak References Jim Baker Resource

    leaks Often you can write code like this, without explicitly calling f.close(): f = open("foo.txt") ... But not always. . .
  19. Write More Robust Code with Weak References Jim Baker Garbage

    collection is not magical GC works by determining that some some set of objects is unreachable: Doesn’t matter if it’s reference counting
  20. Write More Robust Code with Weak References Jim Baker Garbage

    collection is not magical GC works by determining that some some set of objects is unreachable: Doesn’t matter if it’s reference counting Or a variant of mark-and-sweep
  21. Write More Robust Code with Weak References Jim Baker Garbage

    collection is not magical GC works by determining that some some set of objects is unreachable: Doesn’t matter if it’s reference counting Or a variant of mark-and-sweep Or the combination used by CPython, to account for reference cycles
  22. Write More Robust Code with Weak References Jim Baker Takeaway

    It cannot read your mind, developer though you may be!
  23. Write More Robust Code with Weak References Jim Baker Takeaway

    It cannot read your mind, developer though you may be! GC is not sufficient to manage the lifecycle of resources
  24. Write More Robust Code with Weak References Jim Baker Manual

    clearance Clean up resources - setting to None, calling close(), . . .
  25. Write More Robust Code with Weak References Jim Baker Manual

    clearance Clean up resources - setting to None, calling close(), . . . Use try/finally
  26. Write More Robust Code with Weak References Jim Baker try/finally

    try: f = open("foo.txt") ... finally: f.close()
  27. Write More Robust Code with Weak References Jim Baker Manual

    clearance Clean up resources - setting to None, calling close(), . . . Use try/finally Apply deeper knowledge of your code
  28. Write More Robust Code with Weak References Jim Baker Manual

    clearance Clean up resources - setting to None, calling close(), . . . Use try/finally Apply deeper knowledge of your code Or do cleanup by some other scheme
  29. Write More Robust Code with Weak References Jim Baker Finalizers

    with del May use finalizers because of explicit external resource management
  30. Write More Robust Code with Weak References Jim Baker Finalizers

    with del May use finalizers because of explicit external resource management Especially in conjunction with some explicit ref counting
  31. Write More Robust Code with Weak References Jim Baker socket.makefile

    socket.makefile([mode[, bufsize]]) Return a file object associated with the socket. (File objects are described in File Objects.) The file object does not close the socket explicitly when its close() method is called, but only removes its reference to the socket object, so that the socket will be closed if it is not referenced from anywhere else.
  32. Write More Robust Code with Weak References Jim Baker errno.EMFILE?

    Otherwise we may see an IOError raised with errno.EMFILE (“Too many open files”)
  33. Write More Robust Code with Weak References Jim Baker socket.makefile

    socket.makefile([mode[, bufsize]]) Return a file object associated with the socket. (File objects are described in File Objects.) The file object does not close the socket explicitly when its close() method is called, but only removes its reference to the socket object, so that the socket will be closed if it is not referenced from anywhere else. Implementation is done through a separate ref counting scheme
  34. Write More Robust Code with Weak References Jim Baker fileobject

    Prevent resource leaks (of underlying sockets) in the socket module: class _fileobject(object): ... def __del__(self): try: self.close() except: # close() may fail if __init__ didn’t complete pass NB: changed in Python 3.x, above is 2.7 implementation
  35. Write More Robust Code with Weak References Jim Baker with

    statement for ARM You are already using automatic resource management, right? with open("foo.txt") as f: ...
  36. Write More Robust Code with Weak References Jim Baker So

    far, so good No weak references yet
  37. Write More Robust Code with Weak References Jim Baker So

    far, so good No weak references yet Keeping it simple!
  38. Write More Robust Code with Weak References Jim Baker So

    far, so good No weak references yet Keeping it simple! No need to be in this talk, right?
  39. Write More Robust Code with Weak References Jim Baker What

    if. . . An object is a child in a parent-child relationship?
  40. Write More Robust Code with Weak References Jim Baker What

    if. . . An object is a child in a parent-child relationship? And needs to track its parent?
  41. Write More Robust Code with Weak References Jim Baker What

    if. . . An object is a child in a parent-child relationship? And needs to track its parent? And the parent wants to track the child?
  42. Write More Robust Code with Weak References Jim Baker What

    if. . . An object is a child in a parent-child relationship? And needs to track its parent? And the parent wants to track the child? Example: xml.sax.expatreader
  43. Write More Robust Code with Weak References Jim Baker Make

    it even simpler Let’s implement a doubly-linked list - next and previous references
  44. Write More Robust Code with Weak References Jim Baker Make

    it even simpler Let’s implement a doubly-linked list - next and previous references But also add del to clean up resources
  45. Write More Robust Code with Weak References Jim Baker OrderedDict

    Dict that preserves the order of insertion, for iteration and indexed access
  46. Write More Robust Code with Weak References Jim Baker OrderedDict

    Dict that preserves the order of insertion, for iteration and indexed access Asymptotic performance (big-O running time) same as regular dicts
  47. Write More Robust Code with Weak References Jim Baker OrderedDict

    Dict that preserves the order of insertion, for iteration and indexed access Asymptotic performance (big-O running time) same as regular dicts Uses a doubly-linked list to preserve insertion order
  48. Write More Robust Code with Weak References Jim Baker Avoiding

    reference cycles Why is avoiding strong reference cycles important?
  49. Write More Robust Code with Weak References Jim Baker Avoiding

    reference cycles Why is avoiding strong reference cycles important? CPython’s GC usually does reference counting
  50. Write More Robust Code with Weak References Jim Baker Avoiding

    reference cycles Why is avoiding strong reference cycles important? CPython’s GC usually does reference counting But a cycle cannot go to zero
  51. Write More Robust Code with Weak References Jim Baker Under

    the hood CPython’s weak reference scheme stores a list of containers to be cleared out, including proxies
  52. Write More Robust Code with Weak References Jim Baker Under

    the hood CPython’s weak reference scheme stores a list of containers to be cleared out, including proxies Performed when the referred object is deallocated
  53. Write More Robust Code with Weak References Jim Baker Under

    the hood CPython’s weak reference scheme stores a list of containers to be cleared out, including proxies Performed when the referred object is deallocated Which occurs when the refcount goes to zero
  54. Write More Robust Code with Weak References Jim Baker Under

    the hood CPython’s weak reference scheme stores a list of containers to be cleared out, including proxies Performed when the referred object is deallocated Which occurs when the refcount goes to zero No waiting on the garbage collector!
  55. Write More Robust Code with Weak References Jim Baker Example:

    set From setobject.c in CPython 3.5 static void set_dealloc(PySetObject *so) { setentry *entry; Py_ssize_t fill = so->fill; PyObject_GC_UnTrack(so); Py_TRASHCAN_SAFE_BEGIN(so) if (so->weakreflist != NULL) PyObject_ClearWeakRefs((PyObject *) so); ... Also explains why many lightweight objects in CPython are not weak referenceable - avoid the cost of extra overhead of the weakreflist
  56. Write More Robust Code with Weak References Jim Baker Ref

    cycles using GC in CPython Strong reference cycles have to wait for mark-and-sweep GC
  57. Write More Robust Code with Weak References Jim Baker Ref

    cycles using GC in CPython Strong reference cycles have to wait for mark-and-sweep GC CPython’s GC is stop-the-world
  58. Write More Robust Code with Weak References Jim Baker Ref

    cycles using GC in CPython Strong reference cycles have to wait for mark-and-sweep GC CPython’s GC is stop-the-world Runs only per decision criteria in the gc.set threshold, which is now generational
  59. Write More Robust Code with Weak References Jim Baker Ref

    cycles using GC in CPython Strong reference cycles have to wait for mark-and-sweep GC CPython’s GC is stop-the-world Runs only per decision criteria in the gc.set threshold, which is now generational Doesn’t occur when you need it to close that file, or some other issue
  60. Write More Robust Code with Weak References Jim Baker Useful

    points to consider My experience with garbage collectors is that they work well, except when they don’t
  61. Write More Robust Code with Weak References Jim Baker Useful

    points to consider My experience with garbage collectors is that they work well, except when they don’t Especially around a small object pointing to an expensive resource
  62. Write More Robust Code with Weak References Jim Baker Useful

    points to consider My experience with garbage collectors is that they work well, except when they don’t Especially around a small object pointing to an expensive resource Which you might see with resources that have limits
  63. Write More Robust Code with Weak References Jim Baker Bug!

    http://bugs.python.org/issue9825 For 2.7, removed del in r84725
  64. Write More Robust Code with Weak References Jim Baker Bug!

    http://bugs.python.org/issue9825 For 2.7, removed del in r84725 For 3.2, replaced del with weakrefs in r84727
  65. Write More Robust Code with Weak References Jim Baker Bug!

    http://bugs.python.org/issue9825 For 2.7, removed del in r84725 For 3.2, replaced del with weakrefs in r84727 For 3.4, using del no longer means ref cycles are uncollectable garbage
  66. Write More Robust Code with Weak References Jim Baker Python

    2.7 solution Issue #9825: removed del from the definition of collections.OrderedDict. This prevents user-created self-referencing ordered dictionaries from becoming permanently uncollectable GC garbage. The downside is that removing del means that the internal doubly-linked list has to wait for GC collection rather than freeing memory immediately when the refcnt drops to zero. So this is an important fix - don’t want uncollectable garbage!
  67. Write More Robust Code with Weak References Jim Baker Bug!

    http://bugs.python.org/issue9825 For 2.7, removed del in r84725 For 3.2, replaced del with weakrefs in r84727
  68. Write More Robust Code with Weak References Jim Baker Bug!

    http://bugs.python.org/issue9825 For 2.7, removed del in r84725 For 3.2, replaced del with weakrefs in r84727 For 3.4, using del no longer means ref cycles are uncollectable garbage
  69. Write More Robust Code with Weak References Jim Baker Weak

    references to the rescue! See implementation of collections.OrderedDict
  70. Write More Robust Code with Weak References Jim Baker Crux

    of the code Use slots to minimize overhead - no need for a dict per object here __slots__ = ’prev’, ’next’, ’key’, ’__weakref__’
  71. Write More Robust Code with Weak References Jim Baker Crux

    of the code Use slots to minimize overhead - no need for a dict per object here weakref means that a slots-built class should be weak referenceable __slots__ = ’prev’, ’next’, ’key’, ’__weakref__’
  72. Write More Robust Code with Weak References Jim Baker Crux

    of the code Use slots to minimize overhead - no need for a dict per object here weakref means that a slots-built class should be weak referenceable NB: no-op in implementations like Jython __slots__ = ’prev’, ’next’, ’key’, ’__weakref__’
  73. Write More Robust Code with Weak References Jim Baker Crux

    of the code (2) root.prev = proxy(link)
  74. Write More Robust Code with Weak References Jim Baker Lookup

    tables Want to provide more information about a given object
  75. Write More Robust Code with Weak References Jim Baker Lookup

    tables Want to provide more information about a given object Without extending/monkeypatching it
  76. Write More Robust Code with Weak References Jim Baker Lookup

    tables Want to provide more information about a given object Without extending/monkeypatching it (So no use of dict for extra properties)
  77. Write More Robust Code with Weak References Jim Baker Using

    a dict Could use the object as a key But need to manually clean up the dict when the object is no longer needed
  78. Write More Robust Code with Weak References Jim Baker Using

    a dict Could use the object as a key But need to manually clean up the dict when the object is no longer needed Maybe you know, maybe you don’t. Especially useful for libraries
  79. Write More Robust Code with Weak References Jim Baker WeakKeyDictionary

    Insert the object as the key Associate anything you want as a value - list of proprerties, another object, etc
  80. Write More Robust Code with Weak References Jim Baker WeakKeyDictionary

    Insert the object as the key Associate anything you want as a value - list of proprerties, another object, etc When the object used as key goes away, the value is also cleared out (if nothing else is holding onto it)
  81. Write More Robust Code with Weak References Jim Baker Example:

    Django signals Django uses weak references in the implementation of its signal mechanism: Django includes a “signal dispatcher” which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.
  82. Write More Robust Code with Weak References Jim Baker WeakKeyDictionary

    Avoid computing the senders-receivers coupling on the fly, the easy way: self.sender_receivers_cache = weakref.WeakKeyDicti if use_caching else {}
  83. Write More Robust Code with Weak References Jim Baker WeakValueDictionary

    Why? Used by multiprocessing (track processes), logging (track handlers) , symtable. . .
  84. Write More Robust Code with Weak References Jim Baker WeakValueDictionary

    Why? Used by multiprocessing (track processes), logging (track handlers) , symtable. . . Useful for when you want to track the object by some id, and there should only be one, but once the object is no longer needed, you can let it go
  85. Write More Robust Code with Weak References Jim Baker Object

    lifecycle independence One side may depend on the other, but not vice versa
  86. Write More Robust Code with Weak References Jim Baker Object

    lifecycle independence One side may depend on the other, but not vice versa Use weak references for the independent side - process is terminated, can remove the lookup by process id
  87. Write More Robust Code with Weak References Jim Baker Object

    lifecycle independence One side may depend on the other, but not vice versa Use weak references for the independent side - process is terminated, can remove the lookup by process id -> WeakValueDictionary
  88. Write More Robust Code with Weak References Jim Baker Combining

    both weak keys and weak values? Yes, it does make sense. Both sides are independent.
  89. Write More Robust Code with Weak References Jim Baker Example:

    Mapping Java classes to Python wrappers Jython implements this variant of the Highlander pattern: Map the Java class to Python wrappers (strong ref from using Java code)
  90. Write More Robust Code with Weak References Jim Baker Example:

    Mapping Java classes to Python wrappers Jython implements this variant of the Highlander pattern: Map the Java class to Python wrappers (strong ref from using Java code) Python classes to any using Java class (strong ref from using Python code)
  91. Write More Robust Code with Weak References Jim Baker Example:

    Mapping Java classes to Python wrappers Jython implements this variant of the Highlander pattern: Map the Java class to Python wrappers (strong ref from using Java code) Python classes to any using Java class (strong ref from using Python code) AND there can only be one mapping (or at least should be)
  92. Write More Robust Code with Weak References Jim Baker Either

    might go away Why? Java classes will be garbage collected if no ClassLoader (the parent of the class effectively) or objects of that class exist;
  93. Write More Robust Code with Weak References Jim Baker Either

    might go away Why? Java classes will be garbage collected if no ClassLoader (the parent of the class effectively) or objects of that class exist; But Python usage of this class will be GCed if no usage on the Python side - no subclasses in Python, etc
  94. Write More Robust Code with Weak References Jim Baker Implementations

    Pure Python Recipe available (http://code.activestate.com/recipes/528879-weak-key- and-value-dictionary/) but I haven’t evaluated
  95. Write More Robust Code with Weak References Jim Baker Implementations

    Pure Python Recipe available (http://code.activestate.com/recipes/528879-weak-key- and-value-dictionary/) but I haven’t evaluated Easy Jython version, because of JVM ecosystem
  96. Write More Robust Code with Weak References Jim Baker Jython

    version from jythonlib import MapMaker, dict_builder class WeakKeyValueDictionary(dict): def __new__(cls, *args, **kw): return WeakKeyValueDictionaryBuilder(*args, ** # also add itervaluerefs, valuerefs, # iterkeyrefs, keyrefs
  97. Write More Robust Code with Weak References Jim Baker Hook

    into Google Guava Collections WeakKeyValueDictionaryBuilder = dict_builder( MapMaker().weakKeys().weakValues().makeMap, WeakKeyValueDictionary)