diff options
author | Paweł Jastrzębski <pawelj@iosphe.re> | 2014-09-27 11:44:42 +0200 |
---|---|---|
committer | Paweł Jastrzębski <pawelj@iosphe.re> | 2014-09-27 11:44:42 +0200 |
commit | 108e3511260f1cfddbac1720f54adf12a90515f2 (patch) | |
tree | cfb19e935f0a80b9fe34e140c359b3883fe94bf3 /kcc.py | |
parent | Updated setup (diff) | |
download | kcc-108e3511260f1cfddbac1720f54adf12a90515f2.tar.gz kcc-108e3511260f1cfddbac1720f54adf12a90515f2.tar.bz2 kcc-108e3511260f1cfddbac1720f54adf12a90515f2.zip |
Refactored locking mechanism (close #112)
Diffstat (limited to 'kcc.py')
-rwxr-xr-x | kcc.py | 43 |
1 files changed, 18 insertions, 25 deletions
diff --git a/kcc.py b/kcc.py index a0d5fa2..682e411 100755 --- a/kcc.py +++ b/kcc.py @@ -103,24 +103,22 @@ class QApplicationMessaging(QtWidgets.QApplication): def __init__(self, argv): QtWidgets.QApplication.__init__(self, argv) - self._memory = QtCore.QSharedMemory(self) - self._memory.setKey('KCC') - if self._memory.attach(): - self._running = True - else: - self._running = False - self._memory.create(1) self._key = 'KCC' self._timeout = 1000 - self._server = QtNetwork.QLocalServer(self) - if not self.isRunning(): + self._locked = False + socket = QtNetwork.QLocalSocket(self) + socket.connectToServer(self._key, QtCore.QIODevice.WriteOnly) + if not socket.waitForConnected(self._timeout): + self._server = QtNetwork.QLocalServer(self) # noinspection PyUnresolvedReferences self._server.newConnection.connect(self.handleMessage) self._server.listen(self._key) + else: + self._locked = True + socket.disconnectFromServer() - def shutdown(self): - if self._memory.isAttached(): - self._memory.detach() + def __del__(self): + if not self._locked: self._server.close() def event(self, e): @@ -132,7 +130,7 @@ class QApplicationMessaging(QtWidgets.QApplication): return QtWidgets.QApplication.event(self, e) def isRunning(self): - return self._running + return self._locked def handleMessage(self): socket = self._server.nextPendingConnection() @@ -140,18 +138,13 @@ class QApplicationMessaging(QtWidgets.QApplication): self.messageFromOtherInstance.emit(socket.readAll().data()) def sendMessage(self, message): - if self.isRunning(): - socket = QtNetwork.QLocalSocket(self) - socket.connectToServer(self._key, QtCore.QIODevice.WriteOnly) - if not socket.waitForConnected(self._timeout): - return False - # noinspection PyArgumentList - socket.write(bytes(message, 'UTF-8')) - if not socket.waitForBytesWritten(self._timeout): - return False - socket.disconnectFromServer() - return True - return False + socket = QtNetwork.QLocalSocket(self) + socket.connectToServer(self._key, QtCore.QIODevice.WriteOnly) + socket.waitForConnected(self._timeout) + # noinspection PyArgumentList + socket.write(bytes(message, 'UTF-8')) + socket.waitForBytesWritten(self._timeout) + socket.disconnectFromServer() # Adding signals to QMainWindow |