Presentation is loading. Please wait.

Presentation is loading. Please wait.

Networks and Client/Server Applications

Similar presentations


Presentation on theme: "Networks and Client/Server Applications"— 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 Server Network Client 1 Client 2 Client 3 Client N

3 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

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 States in the Life of a Thread
new start The CPU ready The ready queue Schedules threads for the CPU

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

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

9 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

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
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

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

18 For Friday Review and wrapup


Download ppt "Networks and Client/Server Applications"

Similar presentations


Ads by Google