Skip to content

Commit

Permalink
QMP: add get_events(wait=True) option
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Luiz Capitulino <[email protected]>
  • Loading branch information
Stefan Hajnoczi authored and Luiz Capitulino committed Jun 1, 2011
1 parent e9b4b43 commit 91b8edd
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions QMP/qmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ def __get_sock(self):
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:
return
resp = json.loads(data)
if 'event' in resp:
self.__events.append(resp)
continue
if not only_event:
continue
return resp

error = socket.error
Expand Down Expand Up @@ -106,9 +107,11 @@ def cmd(self, name, args=None, id=None):
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:
Expand All @@ -118,6 +121,8 @@ def get_events(self):
# 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):
Expand Down

0 comments on commit 91b8edd

Please sign in to comment.