@@ -4,7 +4,7 @@ Interfaces to the QEMU monitor.
@copyright: 2008-2010 Red Hat Inc.
"""
-import socket, time, threading, logging
+import socket, time, threading, logging, select
import kvm_utils
try:
import json
@@ -58,7 +58,6 @@ class Monitor:
self.filename = filename
self._lock = threading.RLock()
self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- self._socket.setblocking(False)
try:
self._socket.connect(filename)
@@ -105,13 +104,14 @@ class Monitor:
def _recvall(self):
s = ""
while True:
- try:
+ i, o, e = select.select([self._socket], [], [], 0)
+ if len(i) > 0:
data = self._socket.recv(1024)
- except socket.error:
- break
- if not data:
+ if not data:
+ break
+ s += data
+ else:
break
- s += data
return s
@@ -158,19 +158,19 @@ class HumanMonitor(Monitor):
# Private methods
def _read_up_to_qemu_prompt(self, timeout=20):
- o = ""
- end_time = time.time() + timeout
- while time.time() < end_time:
- try:
+ buf = ""
+ while True:
+ i, o, e = select.select([self._socket], [], [], timeout)
+ if len(i) > 0:
data = self._socket.recv(1024)
- if not data:
- break
- o += data
- if o.splitlines()[-1].split()[-1] == "(qemu)":
- return True, "\n".join(o.splitlines()[:-1])
- except (socket.error, IndexError):
- time.sleep(0.01)
- return False, "\n".join(o.splitlines())
+ buf += data
+ try:
+ if buf.splitlines()[-1].split()[-1] == "(qemu)":
+ return True, "\n".join(buf.splitlines()[:-1])
+ except IndexError:
+ continue
+ else:
+ return False, "\n".join(buf.splitlines())
def _send_command(self, command):