ThomasV 10 years ago
parent
commit
78f5dbb72e
  1. 14
      lib/interface.py

14
lib/interface.py

@ -59,7 +59,7 @@ class TcpInterface(threading.Thread):
self.debug = False # dump network messages. can be changed at runtime using the console self.debug = False # dump network messages. can be changed at runtime using the console
self.message_id = 0 self.message_id = 0
self.response_queue = response_queue self.response_queue = response_queue
self.request_queue = Queue.Queue() self.lock = threading.Lock()
self.unanswered_requests = {} self.unanswered_requests = {}
# request timeouts # request timeouts
self.request_time = False self.request_time = False
@ -84,7 +84,8 @@ class TcpInterface(threading.Thread):
result = response.get('result') result = response.get('result')
if msg_id is not None: if msg_id is not None:
method, params, _id, queue = self.unanswered_requests.pop(msg_id) with self.lock:
method, params, _id, queue = self.unanswered_requests.pop(msg_id)
if queue is None: if queue is None:
queue = self.response_queue queue = self.response_queue
else: else:
@ -251,13 +252,7 @@ class TcpInterface(threading.Thread):
return s return s
def send_request(self, request, response_queue = None): def send_request(self, request, response_queue = None):
'''Queue a request. Blocking only if called from other threads.''' with self.lock:
self.request_queue.put((request, response_queue), threading.current_thread() != self)
def send_requests(self):
'''Sends all queued requests'''
while self.is_connected() and not self.request_queue.empty():
request, response_queue = self.request_queue.get()
method = request.get('method') method = request.get('method')
params = request.get('params') params = request.get('params')
r = {'id': self.message_id, 'method': method, 'params': params} r = {'id': self.message_id, 'method': method, 'params': params}
@ -332,7 +327,6 @@ class TcpInterface(threading.Thread):
while self.connected: while self.connected:
self.maybe_ping() self.maybe_ping()
self.send_requests()
self.get_and_process_one_response() self.get_and_process_one_response()
self.change_status() self.change_status()

Loading…
Cancel
Save