Switch to using context managers for locking.

This commit is contained in:
2014-03-28 22:35:44 +00:00
parent 6ab8688657
commit d3e8ce6d2b

View File

@@ -95,7 +95,7 @@ class TS3Proto():
return self._logger
def connect(self, ip, port=10011, timeout=5):
self.io_lock.acquire()
with self.io_lock:
try:
self._telnet = telnetlib.Telnet(ip, port)
except telnetlib.socket.error:
@@ -105,7 +105,6 @@ class TS3Proto():
self._connected = False
data = self._telnet.read_until("\n\r", self._timeout)
self.io_lock.release()
if data.endswith("TS3\n\r"):
self._connected = True
@@ -126,12 +125,11 @@ class TS3Proto():
commandstr = self.construct_command(command, keys=keys, opts=opts)
self.logger.debug("send_command - %s" % commandstr)
self.io_lock.acquire()
with self.io_lock:
self._telnet.write("%s\n\r" % commandstr)
data = ""
response = self._telnet.read_until("\n\r", self._timeout)
self.io_lock.release()
if not response.startswith("error"):
# what we just got was extra data