Networks and Client/Server Applications Handling Multiple Clients Concurrently
Problem One server handles all clients Other clients will have to wait if one client has a big request Server Network Client 1 Client 2 Client 3 Client N
A One-on-One Therapy Server while True: print('Waiting for connection . . . ') client, address = server.accept() print('... connected from:', address) dr = Doctor() client.send(bytes(dr.greeting()), 'ascii')) message = decode(client.recv(BUFSIZE), 'ascii') if not message: print('Client disconnected') client.close() break else: client.send(bytes(dr.reply(message)), 'ascii')) Other patients must wait for the current patient to finish
Multithreading A modern computer can run several processes or threads concurrently Each thread executes it own algorithm Examples: Edit text in a word processor while a spell checker runs in the background Load an image file in the background while the Web browser lays out and displays a page
Multithreading Threads can share a single hardware processor by swapping (timesharing) Or they can run on separate processors in a multicore system
States in the Life of a Thread new start The CPU ready The ready queue Schedules threads for the CPU
States in the Life of a Thread new start yield or timed out running The CPU run ready The ready queue
States in the Life of a Thread dead new complete start yield or timed out running The CPU run ready The ready queue
The Thread Class Imported from the threading module Interface Thread() # Returns a new instance start() # Places the thread on the ready queue run() # Contains the code for the thread to execute; # pass by default
Using a Thread Define a subclass of Thread with a new run method that contains the code to execute Create an instance and run the start method to activate it
A One-on-One Therapy Server while True: print('Waiting for connection . . . ') client, address = server.accept() print('... connected from:', address) dr = Doctor() client.send(bytes(dr.greeting()), 'ascii')) message = decode(client.recv(BUFSIZE), 'ascii') if not message: print('Client disconnected') client.close() break else: client.send(bytes(dr.reply(message)), 'ascii')) Other patients must wait for the current patient to finish
A One-on-One Therapy Server while True: print('Waiting for connection . . . ') client, addr = server.accept() print('... connected from:', addr) dr = Doctor() handler = ClientHandler(client, dr) handler.start() Each client handler manages a conversation between the doctor and a client Because the client handlers run as separate threads, many of them can run concurrently The server can cycle back immediately to listen for another client
The ClientHandler Class from threading import Thread class ClientHandler(Thread): The ClientHandler class is a subclass of Thread As such, objects of type ClientHandler can be used wherever threads are used
The ClientHandler Class from threading import Thread class ClientHandler(Thread): def __init__(self, client, dr): Thread.__init__(self) self.client = client self.dr = dr The __init__ method runs the __init__ method in the Thread class receives the client socket and the doctor object from the server and transfers these to instance variables
The ClientHandler Class from threading import Thread class ClientHandler(Thread): def __init__(self, client, dr): Thread.__init__(self) self.client = client self.dr = dr def run(self): self.client.send(bytes(self.dr.greeting(), 'ascii')) while True: message = decode(self.client.recv(BUFSIZE), 'ascii') if not message: print('Client disconnected') client.close() break else: self.client.send(bytes(self.dr.reply(message), 'ascii'))
Extensions Create a separate Doctor object for each client Save the client’s history list to a file If a file for a client exists, load the history list from it when she logs in Add separate GUIs for the client and the server
Extensibility The client/server pattern can be applied to many situations, such as the ATM and bank manager applications of this week’s lab Many ATM clients (on many different computers) One server/manager running on a single host
For Friday Review and wrapup