|
Revision 6637, 0.7 kB
(checked in by glyph, 6 years ago)
|
Object database for Twisted. This Checkin Sponsored by Divmod, LLC
|
| Line | |
|---|
| 1 |
|
|---|
| 2 |
""" |
|---|
| 3 |
A module for externalized finalizers. |
|---|
| 4 |
""" |
|---|
| 5 |
|
|---|
| 6 |
import weakref |
|---|
| 7 |
|
|---|
| 8 |
garbageKey = 0 |
|---|
| 9 |
|
|---|
| 10 |
def callbackFactory(num, fins): |
|---|
| 11 |
def _cb(w): |
|---|
| 12 |
del refs[num] |
|---|
| 13 |
for fx in fins: |
|---|
| 14 |
fx() |
|---|
| 15 |
return _cb |
|---|
| 16 |
|
|---|
| 17 |
refs = {} |
|---|
| 18 |
|
|---|
| 19 |
def register(inst): |
|---|
| 20 |
global garbageKey |
|---|
| 21 |
garbageKey += 1 |
|---|
| 22 |
r = weakref.ref(inst, callbackFactory(garbageKey, inst.__finalizers__())) |
|---|
| 23 |
refs[garbageKey] = r |
|---|
| 24 |
|
|---|
| 25 |
if __name__ == '__main__': |
|---|
| 26 |
def fin(): |
|---|
| 27 |
print 'I am _so_ dead.' |
|---|
| 28 |
|
|---|
| 29 |
class Finalizeable: |
|---|
| 30 |
""" |
|---|
| 31 |
An un-sucky __del__ |
|---|
| 32 |
""" |
|---|
| 33 |
|
|---|
| 34 |
def __finalizers__(self): |
|---|
| 35 |
""" |
|---|
| 36 |
I'm going away. |
|---|
| 37 |
""" |
|---|
| 38 |
return [fin] |
|---|
| 39 |
|
|---|
| 40 |
f = Finalizeable() |
|---|
| 41 |
f.f2 = f |
|---|
| 42 |
register(f) |
|---|
| 43 |
del f |
|---|
| 44 |
import gc |
|---|
| 45 |
gc.collect() |
|---|
| 46 |
print 'deled' |
|---|