optionally start asyncore loop in thread in tcp client/server start
This commit is contained in:
@@ -12,10 +12,10 @@ sock.bind(server_address)
|
||||
while True:
|
||||
print >>sys.stderr, '\nwaiting to receive message'
|
||||
data, address = sock.recvfrom(4096)
|
||||
|
||||
|
||||
print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
|
||||
print >>sys.stderr, data
|
||||
|
||||
|
||||
if data:
|
||||
sent = sock.sendto(data, address)
|
||||
print >>sys.stderr, 'sent %s bytes back to %s' % (sent, address)
|
||||
@@ -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)
|
||||
|
||||
@@ -16,16 +16,19 @@ class TcpServer(asyncore.dispatcher):
|
||||
print 'Connected from', address
|
||||
self.clients.append(TcpServerClient(self, mysocket, self.bufferSize))
|
||||
|
||||
def start(self):
|
||||
def start(self, thread=True):
|
||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
self.bind(self.address)
|
||||
self.listen(5)
|
||||
self.thread = threading.Thread(target=asyncore.loop, kwargs = {'timeout': 0.5})
|
||||
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, message):
|
||||
for client in self.clients:
|
||||
|
||||
@@ -1,45 +0,0 @@
|
||||
import socket
|
||||
import threading
|
||||
import SocketServer
|
||||
|
||||
class ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
|
||||
|
||||
def handle(self):
|
||||
data = self.request.recv(1024)
|
||||
cur_thread = threading.current_thread()
|
||||
response = "{}: {}".format(cur_thread.name, data)
|
||||
self.request.sendall(response)
|
||||
|
||||
class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
|
||||
pass
|
||||
|
||||
def client(ip, port, message):
|
||||
sock = socket.mysocket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.connect((ip, port))
|
||||
try:
|
||||
sock.sendall(message)
|
||||
response = sock.recv(1024)
|
||||
print "Received: {}".format(response)
|
||||
finally:
|
||||
sock.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
# Port 0 means to select an arbitrary unused port
|
||||
HOST, PORT = "localhost", 0
|
||||
|
||||
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
|
||||
ip, port = server.server_address
|
||||
|
||||
# Start a thread with the server -- that thread will then start one
|
||||
# more thread for each request
|
||||
server_thread = threading.Thread(target=server.serve_forever)
|
||||
# Exit the server thread when the main thread terminates
|
||||
server_thread.daemon = True
|
||||
server_thread.start()
|
||||
print "Server loop running in thread:", server_thread.name
|
||||
|
||||
client(ip, port, "Hello World 1")
|
||||
client(ip, port, "Hello World 2")
|
||||
client(ip, port, "Hello World 3")
|
||||
|
||||
server.shutdown()
|
||||
Reference in New Issue
Block a user