Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networks and Client/Server Applications Handling Multiple Clients Concurrently.

Similar presentations


Presentation on theme: "Networks and Client/Server Applications Handling Multiple Clients Concurrently."— Presentation transcript:

1 Networks and Client/Server Applications Handling Multiple Clients Concurrently

2 Problem One server handles all clients Other clients will have to wait if one client has a big request Client 1Client 2Client 3Client N Network Server

3 A One-on-One Therapy Server Other patients must wait for the current patient to finish 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'))

4 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

5 Multithreading Threads can share a single hardware processor by swapping (timesharing) Or they can run on separate processors in a multicore system

6 new ready start The ready queue States in the Life of a Thread Schedules threads for the CPU The CPU

7 new ready running start run yield or timed out The ready queue The CPU States in the Life of a Thread

8 new ready running dead start complete run yield or timed out The ready queue The CPU States in the Life of a Thread

9 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 The Thread Class Interface Imported from the threading module

10 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

11 A One-on-One Therapy Server Other patients must wait for the current patient to finish 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'))

12 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

13 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

14 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

15 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'))

16 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

17 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


Download ppt "Networks and Client/Server Applications Handling Multiple Clients Concurrently."

Similar presentations


Ads by Google