From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:57986) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPVXr-0008C6-4U for qemu-devel@nongnu.org; Thu, 26 May 2011 04:03:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QPVXq-00083U-4P for qemu-devel@nongnu.org; Thu, 26 May 2011 04:03:51 -0400 Received: from mail-gw0-f45.google.com ([74.125.83.45]:65366) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QPVXq-00083P-0B for qemu-devel@nongnu.org; Thu, 26 May 2011 04:03:50 -0400 Received: by gwb19 with SMTP id 19so230165gwb.4 for ; Thu, 26 May 2011 01:03:49 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20110525181522.42eb9a70@doriath> References: <1306349281-16913-1-git-send-email-stefanha@linux.vnet.ibm.com> <20110525181522.42eb9a70@doriath> Date: Thu, 26 May 2011 09:03:49 +0100 Message-ID: From: Stefan Hajnoczi Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [Qemu-devel] [PATCH 1/2] QMP: add get_events(wait=True) option List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Luiz Capitulino Cc: Stefan Hajnoczi , qemu-devel@nongnu.org On Wed, May 25, 2011 at 10:15 PM, Luiz Capitulino wrote: > On Wed, 25 May 2011 19:48:00 +0100 > Stefan Hajnoczi wrote: > >> The get_events() function polls for new QMP events and then returns. =A0= It >> can be useful to wait for the next QMP event so add the boolean 'wait' >> keyword argument. >> >> Signed-off-by: Stefan Hajnoczi >> --- >> =A0QMP/qmp.py | =A0 11 ++++++++--- >> =A01 files changed, 8 insertions(+), 3 deletions(-) >> >> diff --git a/QMP/qmp.py b/QMP/qmp.py >> index 14ce8b0..2565508 100644 >> --- a/QMP/qmp.py >> +++ b/QMP/qmp.py >> @@ -43,7 +43,7 @@ class QEMUMonitorProtocol: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0family =3D socket.AF_UNIX >> =A0 =A0 =A0 =A0 =A0return socket.socket(family, socket.SOCK_STREAM) >> >> - =A0 =A0def __json_read(self): >> + =A0 =A0def __json_read(self, only_event=3DFalse): >> =A0 =A0 =A0 =A0 =A0while True: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0data =3D self.__sockfile.readline() >> =A0 =A0 =A0 =A0 =A0 =A0 =A0if not data: >> @@ -51,7 +51,8 @@ class QEMUMonitorProtocol: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0resp =3D json.loads(data) >> =A0 =A0 =A0 =A0 =A0 =A0 =A0if 'event' in resp: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0self.__events.append(resp) >> - =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0continue >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0if not only_event: >> + =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0continue >> =A0 =A0 =A0 =A0 =A0 =A0 =A0return resp >> >> =A0 =A0 =A0error =3D socket.error >> @@ -106,9 +107,11 @@ class QEMUMonitorProtocol: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0qmp_cmd['id'] =3D id >> =A0 =A0 =A0 =A0 =A0return self.cmd_obj(qmp_cmd) >> >> - =A0 =A0def get_events(self): >> + =A0 =A0def get_events(self, wait=3DFalse): >> =A0 =A0 =A0 =A0 =A0""" >> =A0 =A0 =A0 =A0 =A0Get a list of available QMP events. >> + >> + =A0 =A0 =A0 =A0@param wait: block until an event is available (bool) >> =A0 =A0 =A0 =A0 =A0""" >> =A0 =A0 =A0 =A0 =A0self.__sock.setblocking(0) >> =A0 =A0 =A0 =A0 =A0try: >> @@ -118,6 +121,8 @@ class QEMUMonitorProtocol: >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0# No data available >> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0pass >> =A0 =A0 =A0 =A0 =A0self.__sock.setblocking(1) >> + =A0 =A0 =A0 =A0if not self.__events and wait: >> + =A0 =A0 =A0 =A0 =A0 =A0self.__json_read(only_event=3DTrue) >> =A0 =A0 =A0 =A0 =A0return self.__events > > Maybe this is better (untested): I've tested it because that's how I implemented it first too :). However, I don't want to block until the QMP monitor sends the next event when there is already a received event pending. The patch I posted polls for new events first, then only blocks if there are no events available. Stefan