200 likes | 350 Views
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:
E N D
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')) while True: 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
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')) while True: 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 a 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 defrun(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'))
A Multi-Client Chat Room • The server supports the chat but does not participate in it • Clients are identified by name (or pseudonym) • Many clients can send messages simultaneously • Each message is time-stamped and name-stamped • A reply from the server includes a complete record of the conversation thus far (time, user name, message)
The Conversation Database • A database maintains a record of the conversation • The server creates and passes this common database to each client handler • A client handler adds clients’ messages to the database and retrieves the record to send to clients
The Structure of the System Server Database Handler 1 Handler 2 Handler 3 Handler N Client 1 Client 2 Client 3 Client N
The Client’s Task • The client prompts the user for her name • The client sends this name upon connecting to the server • The client receives a record of the conversation from the server • The client is prompted for a message • If the client simply presses Enter, close the connection and break • Otherwise, the message is sent to the server • Goto step 3
The Client Handler’s Task • The handler receives the client’s name and saves it in an instance variable • The handler retrieves the record from the database and sends it to the client • The handler receives the client’s message • If the message is empty, close the connection and break • Otherwise, the handler name-stamps and time-stamps the message • The handler adds this message to the database • Goto step 2