Skip to content

Implement context manager in EventManager #157

Open
@enzbang

Description

@enzbang

We very often need to do the following:

      send_event(e)  # notify start
      ...
      e.close()
      send_event(e)  # notify end

We could factorize such code in EventManager, e.g. with:

+++ b/e3/event/backends/base.py
@@ -1,6 +1,7 @@
 from __future__ import absolute_import, division, print_function

 import abc
+import contextlib
 import uuid

 import e3.env
@@ -79,3 +80,13 @@ class EventManager(object):
     def Event(self):
         """Return the Event class used by this EventManager."""
         pass  # all: no cover
+
+    @contextlib.contextmanager
+    def with_event(self, *args, **kwargs):
+        e = self.Event(*args, **kwargs)
+        try:
+            self.send_event(e)
+            yield e
+        finally:
+            e.close()
+            self.send_event(e)

Code not tested!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions