Message ID | 1306349281-16913-1-git-send-email-stefanha@linux.vnet.ibm.com |
---|---|
State | New |
Headers | show |
On Wed, 25 May 2011 19:48:00 +0100 Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> wrote: > The get_events() function polls for new QMP events and then returns. It > can be useful to wait for the next QMP event so add the boolean 'wait' > keyword argument. > > Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> > --- > QMP/qmp.py | 11 ++++++++--- > 1 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: > family = socket.AF_UNIX > return socket.socket(family, socket.SOCK_STREAM) > > - def __json_read(self): > + def __json_read(self, only_event=False): > while True: > data = self.__sockfile.readline() > if not data: > @@ -51,7 +51,8 @@ class QEMUMonitorProtocol: > resp = json.loads(data) > if 'event' in resp: > self.__events.append(resp) > - continue > + if not only_event: > + continue > return resp > > error = socket.error > @@ -106,9 +107,11 @@ class QEMUMonitorProtocol: > qmp_cmd['id'] = id > return self.cmd_obj(qmp_cmd) > > - def get_events(self): > + def get_events(self, wait=False): > """ > Get a list of available QMP events. > + > + @param wait: block until an event is available (bool) > """ > self.__sock.setblocking(0) > try: > @@ -118,6 +121,8 @@ class QEMUMonitorProtocol: > # No data available > pass > self.__sock.setblocking(1) > + if not self.__events and wait: > + self.__json_read(only_event=True) > return self.__events Maybe this is better (untested): def get_events(self, wait=False): """ Get a list of available QMP events. @param wait: block until an event is available (bool) """ if not wait: self.__sock.setblocking(0) try: self.__json_read(only_event=wait) except socket.error, err: if err[0] == errno.EAGAIN: # No data available pass if not wait: self.__sock.setblocking(1) return self.__events > > def clear_events(self):
On Wed, May 25, 2011 at 10:15 PM, Luiz Capitulino <lcapitulino@redhat.com> wrote: > On Wed, 25 May 2011 19:48:00 +0100 > Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> wrote: > >> The get_events() function polls for new QMP events and then returns. It >> can be useful to wait for the next QMP event so add the boolean 'wait' >> keyword argument. >> >> Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> >> --- >> QMP/qmp.py | 11 ++++++++--- >> 1 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: >> family = socket.AF_UNIX >> return socket.socket(family, socket.SOCK_STREAM) >> >> - def __json_read(self): >> + def __json_read(self, only_event=False): >> while True: >> data = self.__sockfile.readline() >> if not data: >> @@ -51,7 +51,8 @@ class QEMUMonitorProtocol: >> resp = json.loads(data) >> if 'event' in resp: >> self.__events.append(resp) >> - continue >> + if not only_event: >> + continue >> return resp >> >> error = socket.error >> @@ -106,9 +107,11 @@ class QEMUMonitorProtocol: >> qmp_cmd['id'] = id >> return self.cmd_obj(qmp_cmd) >> >> - def get_events(self): >> + def get_events(self, wait=False): >> """ >> Get a list of available QMP events. >> + >> + @param wait: block until an event is available (bool) >> """ >> self.__sock.setblocking(0) >> try: >> @@ -118,6 +121,8 @@ class QEMUMonitorProtocol: >> # No data available >> pass >> self.__sock.setblocking(1) >> + if not self.__events and wait: >> + self.__json_read(only_event=True) >> return 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
On Thu, 26 May 2011 09:03:49 +0100 Stefan Hajnoczi <stefanha@gmail.com> wrote: > On Wed, May 25, 2011 at 10:15 PM, Luiz Capitulino > <lcapitulino@redhat.com> wrote: > > On Wed, 25 May 2011 19:48:00 +0100 > > Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> wrote: > > > >> The get_events() function polls for new QMP events and then returns. It > >> can be useful to wait for the next QMP event so add the boolean 'wait' > >> keyword argument. > >> > >> Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> > >> --- > >> QMP/qmp.py | 11 ++++++++--- > >> 1 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: > >> family = socket.AF_UNIX > >> return socket.socket(family, socket.SOCK_STREAM) > >> > >> - def __json_read(self): > >> + def __json_read(self, only_event=False): > >> while True: > >> data = self.__sockfile.readline() > >> if not data: > >> @@ -51,7 +51,8 @@ class QEMUMonitorProtocol: > >> resp = json.loads(data) > >> if 'event' in resp: > >> self.__events.append(resp) > >> - continue > >> + if not only_event: > >> + continue > >> return resp > >> > >> error = socket.error > >> @@ -106,9 +107,11 @@ class QEMUMonitorProtocol: > >> qmp_cmd['id'] = id > >> return self.cmd_obj(qmp_cmd) > >> > >> - def get_events(self): > >> + def get_events(self, wait=False): > >> """ > >> Get a list of available QMP events. > >> + > >> + @param wait: block until an event is available (bool) > >> """ > >> self.__sock.setblocking(0) > >> try: > >> @@ -118,6 +121,8 @@ class QEMUMonitorProtocol: > >> # No data available > >> pass > >> self.__sock.setblocking(1) > >> + if not self.__events and wait: > >> + self.__json_read(only_event=True) > >> return 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. Ok, pushed both patches to the QMP queue. Thanks.
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: family = socket.AF_UNIX return socket.socket(family, socket.SOCK_STREAM) - def __json_read(self): + def __json_read(self, only_event=False): while True: data = self.__sockfile.readline() if not data: @@ -51,7 +51,8 @@ class QEMUMonitorProtocol: resp = json.loads(data) if 'event' in resp: self.__events.append(resp) - continue + if not only_event: + continue return resp error = socket.error @@ -106,9 +107,11 @@ class QEMUMonitorProtocol: qmp_cmd['id'] = id return self.cmd_obj(qmp_cmd) - def get_events(self): + def get_events(self, wait=False): """ Get a list of available QMP events. + + @param wait: block until an event is available (bool) """ self.__sock.setblocking(0) try: @@ -118,6 +121,8 @@ class QEMUMonitorProtocol: # No data available pass self.__sock.setblocking(1) + if not self.__events and wait: + self.__json_read(only_event=True) return self.__events def clear_events(self):
The get_events() function polls for new QMP events and then returns. It can be useful to wait for the next QMP event so add the boolean 'wait' keyword argument. Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> --- QMP/qmp.py | 11 ++++++++--- 1 files changed, 8 insertions(+), 3 deletions(-)