optionally start asyncore loop in thread in tcp client/server start

This commit is contained in:
2015-09-12 09:13:43 +01:00
parent aee68f8c7c
commit 5ab020f8af
4 changed files with 15 additions and 55 deletions

View File

@@ -28,15 +28,17 @@ class TcpClient(asyncore.dispatcher):
self.send(data)
self.queue = []
def start(self):
def start(self, thread=True):
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect(self.address)
self.thread = threading.Thread(target=asyncore.loop, kwargs = {'timeout': 1})
self.thread.start()
if thread:
self.thread = threading.Thread(target=asyncore.loop, kwargs = {'timeout': 1})
self.thread.start()
def stop(self):
self.close()
self.thread.join()
if self.thread:
self.thread.join()
def write(self, data):
self.queue.append(data)