diff mbox

[v2,32/45] qmp: add pull_event function

Message ID 1348675011-8794-33-git-send-email-pbonzini@redhat.com
State New
Headers show

Commit Message

Paolo Bonzini Sept. 26, 2012, 3:56 p.m. UTC
This function is unlike get_events in that it makes it easy to process
one event at a time.  This is useful in the mirroring test cases, where
we want to process just one event (BLOCK_JOB_ERROR) and leave the others
to a helper function.

Cc: Luiz Capitulino <lcapitulino@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 QMP/qmp.py | 20 ++++++++++++++++++++
 1 file modificato, 20 inserzioni(+)

Comments

Luiz Capitulino Sept. 26, 2012, 5:17 p.m. UTC | #1
On Wed, 26 Sep 2012 17:56:38 +0200
Paolo Bonzini <pbonzini@redhat.com> wrote:

> This function is unlike get_events in that it makes it easy to process
> one event at a time.  This is useful in the mirroring test cases, where
> we want to process just one event (BLOCK_JOB_ERROR) and leave the others
> to a helper function.
> 
> Cc: Luiz Capitulino <lcapitulino@redhat.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Acked-by: Luiz Capitulino <lcapitulino@redhat.com>

> ---
>  QMP/qmp.py | 20 ++++++++++++++++++++
>  1 file modificato, 20 inserzioni(+)
> 
> diff --git a/QMP/qmp.py b/QMP/qmp.py
> index 36ecc1d..7a598f1 100644
> --- a/QMP/qmp.py
> +++ b/QMP/qmp.py
> @@ -134,6 +134,26 @@ class QEMUMonitorProtocol:
>              raise Exception(ret['error']['desc'])
>          return ret['return']
>  
> +    def pull_event(self, wait=False):
> +        """
> +        Get and delete the first available QMP event.
> +
> +        @param wait: block until an event is available (bool)
> +        """
> +        self.__sock.setblocking(0)
> +        try:
> +            self.__json_read()
> +        except socket.error, err:
> +            if err[0] == errno.EAGAIN:
> +                # No data available
> +                pass
> +        self.__sock.setblocking(1)
> +        if not self.__events and wait:
> +            self.__json_read(only_event=True)
> +        event = self.__events[0]
> +        del self.__events[0]
> +        return event
> +
>      def get_events(self, wait=False):
>          """
>          Get a list of available QMP events.
diff mbox

Patch

diff --git a/QMP/qmp.py b/QMP/qmp.py
index 36ecc1d..7a598f1 100644
--- a/QMP/qmp.py
+++ b/QMP/qmp.py
@@ -134,6 +134,26 @@  class QEMUMonitorProtocol:
             raise Exception(ret['error']['desc'])
         return ret['return']
 
+    def pull_event(self, wait=False):
+        """
+        Get and delete the first available QMP event.
+
+        @param wait: block until an event is available (bool)
+        """
+        self.__sock.setblocking(0)
+        try:
+            self.__json_read()
+        except socket.error, err:
+            if err[0] == errno.EAGAIN:
+                # No data available
+                pass
+        self.__sock.setblocking(1)
+        if not self.__events and wait:
+            self.__json_read(only_event=True)
+        event = self.__events[0]
+        del self.__events[0]
+        return event
+
     def get_events(self, wait=False):
         """
         Get a list of available QMP events.