do lock with the with statement

This commit is contained in:
Bastian Schroll 2018-01-12 09:50:46 +01:00
parent 4dad6bc414
commit 7662f05f0c

View file

@ -22,7 +22,8 @@ import time
logging.debug("- %s loaded", __name__) logging.debug("- %s loaded", __name__)
_clients = [] # module wide global list for received data sets _clients = [] # module wide global list for received data sets
lock = threading.Lock() _lockClients = threading.Lock()
class TCPHandler(socketserver.BaseRequestHandler): class TCPHandler(socketserver.BaseRequestHandler):
"""!RequestHandler class for our TCPServer class.""" """!RequestHandler class for our TCPServer class."""
@ -43,9 +44,10 @@ class TCPHandler(socketserver.BaseRequestHandler):
# add a new entry at first position (index 0) with client IP # add a new entry at first position (index 0) with client IP
# and the decoded data dict as an string in utf-8 # and the decoded data dict as an string in utf-8
lock.acquire() # todo check if needed - only append not modify data # lock.acquire() # todo check if needed - only append not modify data
_clients.insert(0, (self.client_address[0], data, time.time())) # time() to calc time in queue with _lockClients:
lock.release() # todo check if needed _clients.insert(0, (self.client_address[0], data, time.time())) # time() to calc time in queue
# lock.release() # todo check if needed
logging.debug("Add data to queue") logging.debug("Add data to queue")
logging.debug(req_name + " send: [ack]") logging.debug(req_name + " send: [ack]")
@ -131,9 +133,10 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
@return Next data packet.py from intern queue""" @return Next data packet.py from intern queue"""
if _clients: if _clients:
lock.acquire() # todo check if needed - only append not modify data # lock.acquire() # todo check if needed - only append not modify data
message = _clients.pop() with _lockClients:
lock.release() # todo check if needed message = _clients.pop()
# lock.release() # todo check if needed
logging.debug("Get data from queue") logging.debug("Get data from queue")
return message return message
return None return None
@ -141,7 +144,8 @@ class TCPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
@staticmethod @staticmethod
def flushData(): def flushData():
"""!To flush all existing data in queue""" """!To flush all existing data in queue"""
logging.debug("Flush client data queue") logging.debug("Flush data queue")
lock.acquire() # todo check if needed - here is a modify? # lock.acquire() # todo check if needed - here is a modify?
_clients.clear() with _lockClients:
lock.release() # todo check if needed _clients.clear()
# lock.release() # todo check if needed