diff options
author | Isaku Yamahata <yamahata@valinux.co.jp> | 2012-02-07 15:17:34 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2012-02-09 21:57:55 +0900 |
commit | c597ecf1b75cd2ad6ba5def615eeb09a66dedbb1 (patch) | |
tree | caefab4d4b61ae76f63e5d94f64aa912b0cf213d | |
parent | b9ddde883012f8b455a0ce475c392fcab7bf309b (diff) |
controller/dispatcher: eliminate the use of WeakSet
As weakref.WeakSet is supported by python 2.7+. And RHEL 6.x uses python 2.6.x
So avoid weakref.WeakSet by replacing it with WeakValueDictionary which is
supported by weakref package from the beginning.
Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/app/event_dumper.py | 2 | ||||
-rw-r--r-- | ryu/controller/dispatcher.py | 41 |
2 files changed, 25 insertions, 18 deletions
diff --git a/ryu/app/event_dumper.py b/ryu/app/event_dumper.py index 977b34ad..e881c5fd 100644 --- a/ryu/app/event_dumper.py +++ b/ryu/app/event_dumper.py @@ -32,7 +32,7 @@ class EventDumper(object): def __init__(self, *_args, **_kwargs): # EventDispatcher can be created and cloned before us. # So register it explicitly - for ev_q in dispatcher.EventQueue.event_queues: + for ev_q in dispatcher.EventQueue.event_queues.values(): if ev_q == dispatcher.QUEUE_EV_Q: continue LOG.info('%s: registering q %s dispatcher %s', diff --git a/ryu/controller/dispatcher.py b/ryu/controller/dispatcher.py index 7b0f02aa..6bef53a9 100644 --- a/ryu/controller/dispatcher.py +++ b/ryu/controller/dispatcher.py @@ -22,13 +22,20 @@ from . import event LOG = logging.getLogger('ryu.controller.dispatcher') +# WeakSet is supported by python 2.7+. So WeakValueDictionary is used +# instead for python 2.6 which is used by REHL6 +# e.g. +# wvd = WeakValueDictionary() ws = WeakSet() +# wvd[id(value)] = value ws = value +# wvd.values() ws: iterator + class EventQueue(object): - # WeakSet: This set is populated by __init__(). - # So need to use weak reference in order to make instances - # freeable by avoiding refrence count. - # Otherwise, instances can't be freed. - event_queues = weakref.WeakSet() + # WeakValueDictionary: This set is populated by __init__(). + # So need to use weak reference in order to make + # instances freeable by avoiding refrence count. + # Otherwise, instances can't be freed. + event_queues = weakref.WeakValueDictionary() # weakref: break circular reference # self._ev_q_weakref == weakref.ref(self) @@ -55,7 +62,7 @@ class EventQueue(object): self.ev_q = Queue() self.aux = aux # for EventQueueCreate event - self.event_queues.add(self) + self.event_queues[id(self)] = self self._queue_q_ev(EventQueueCreate(self, True)) def __del__(self): @@ -101,29 +108,29 @@ class EventQueue(object): class EventDispatcher(object): - # WeakSet: This set is populated by __init__(). - # So need to use weak reference in order to make instances - # freeable by avoiding refrence count. - # Otherwise, instances can't be freed. - event_dispatchers = weakref.WeakSet() + # WeakValueDictionary: This set is populated by __init__(). + # So need to use weak reference in order to make + # instances freeable by avoiding refrence count. + # Otherwise, instances can't be freed. + event_dispatchers = weakref.WeakValueDictionary() def __init__(self, name): - # WeakSet: In order to let child to go away. - # We are interested only in alive children. - self.children = weakref.WeakSet() + # WeakValueDictionary: In order to let child to go away. + # We are interested only in alive children. + self.children = weakref.WeakValueDictionary() self.name = name self.events = {} self.inheritable_events = {} self.all_handlers = [] - self.event_dispatchers.add(self) + self.event_dispatchers[id(self)] = self def clone(self, old_dispatcher=None): cloned = EventDispatcher(self.name) for ev_cls, h in self.events.items(): cloned.events[ev_cls] = copy.copy(h) cloned.all_handlers = copy.copy(self.all_handlers) - self.children.add(cloned) + self.children[id(cloned)] = cloned if old_dispatcher is not None: cloned.inheritable_events = old_dispatcher.inheritable_events @@ -132,7 +139,7 @@ class EventDispatcher(object): return cloned def _foreach_children(self, call, *args, **kwargs): - for c in self.children: + for c in self.children.values(): call(c, *args, **kwargs) def register_all_handler(self, all_handler): |