CS302.
3
Wireless Technologies and
Network Programming
Lecture 12 – Iterative vs Concurrent Services with Python
Chamara Disanayake
Department of Network and Security
Iterative Servers
• Servers can be categorized as either iterative, concurrent, or reactive. The
primary trade-offs in this dimension involve simplicity of programming
versus the ability to scale to increased service offerings and host loads.
• Iterative servers handle each client request in its entirety before servicing
subsequent requests.
• While processing a request, an iterative server therefore either queues or
ignores additional requests . Iterative servers are best suited for either
• Short-duration services, such as the standard Internet ECHO and DAYTIME services,
that have minimal execution time variation
• Infrequently run services, such as a remote file system backup service that runs
nightly when platforms are lightly loaded
2
Iterative Servers
• Requests Handling procedure
• The server receives the incoming connection.
• The server handles the connection.
• The server closes the connection.
• The server returns to listening on its well-known port.
3
Handling Multiple Client Requests , one at a time (Iterative Server)
• Example: Time Server which sends the network time to client when
requested
• Server program (an iterative server) that accept connection from
client, send network time and close client connection.
• Client connect to server and get time from the server and display the
time.
Res
Req
Res
Req
Time
Iterative Server Program
Definition of
function
It Allows You to Execute Code When the
File Runs as a Script, but Not When It’s
What is THIS?
Imported as a Module
Client Program
6
Executing programs
• Server program
• Client Program
Exercise
• Modify the program and check the outcome.
• Change the program to align with a client server real world communication.
You can decide a real-world scenario and modify the code and observe the
outcome.
• What are the services which can be used with Iterative service
model?
• How it can be implemented?
• What modifications need to be done?
• Do we need to change the client side?
8
Concurrent Programming
• Concurrency means multiple computations are happening at the
same time.
• Concurrency is everywhere in modern programming, whether we like it or
not: • Multiple computers in a network
• Multiple applications running on one computer
• Multiple processors in a computer (today, often multiple processor cores on a single chip)
• In fact, concurrency is essential in modern programming:
• Web sites must handle multiple simultaneous users.
• Mobile apps need to do some of their processing on servers (“in the cloud”).
• Graphical user interfaces almost always require background work that does not interrupt the
user. For example, Eclipse compiles your Java code while you’re still editing it.
9
How Concurrency is Implemented
• Concurrency is available in any mode of Processor architectures
• concurrent computing
enables executing of two
or more processes in an
interleaved fashion
through context
switching and complete
in an overlapping time
period by managing
This doesn’t necessarily mean that multiple
access to shared processes will be running at the same instant –
resources even if the results might make it seem like it.
10
Concurrency in Python
• Concurrent programming in Python refers to executing two or more
tasks at the same time.
• Python provides three main types of concurrent programming, each
centered around a different unit of concurrency and each in a
separate Python module.
• Python multiprocessing for process-based concurrency.
• Python threading for thread-based concurrency.
• Python asyncio for coroutine-based concurrency.
11
Thread-Based Concurrency
• A thread refers to a thread of execution by a computer program.
• Every Python program is a process with one thread called the main thread
used to execute your program instructions.
• We can create new threads to run concurrently within our Python program.
• Multiple threads within a process share the same data space with the main
thread and can therefore share information or communicate with each other
more easily than if they were separate processes.
• Threads sometimes called light-weight processes and they do not require
much memory overhead; they are cheaper than processes.
• Python provides real or native (system-level) threads via the
threading.Thread class.
12
Functions used with Threads
• There are two modules which support the usage of threads in
Python3 −
• _thread : ‘thread’ module used earlier was renamed to "_thread" for
backwards compatibilities in Python3.
• threading : The newer threading module included with Python 2.4 provides
much more powerful, high-level support for threads than the thread module
• To spawn another thread, you need to call following method available
in thread module
thread.start_new_thread ( function, args[, kwargs] )
• The method call returns immediately and the child thread starts and calls
function with the passed list of args. When the function returns, the thread
terminates.
13
Features with _threading Module
• The threading module exposes all the methods of the thread module and
provides some additional methods −
threading.activeCount() − Returns the number of thread objects that are active.
threading.currentThread() − Returns the number of thread objects in the caller's thread control.
threading.enumerate() − Returns a list of all thread objects that are currently active.
• In addition to the methods, the threading module has the Thread class that
implements threading. The methods provided by the Thread class are as follows
run() − The run() method is the entry point for a thread.
start() − The start() method starts a thread by calling the run method.
join([time]) − The join() waits for threads to terminate.
isAlive() − The isAlive() method checks whether a thread is still executing.
getName() − The getName() method returns the name of a thread.
setName() − The setName() method sets the name of a thread.
14
Concurrent Server
• In an iterative server, each client request has the wait till the first
request finishes.
• With Concurrent server, it allows to process client requests with
variable service time.
• When a client request is received, the server process spawns a separate
thread, which is exclusively meant to handle the particular client.
• So, if a program has a I/O blocking at a time, it would be the particular thread
(doing the process) that will wait and not the whole server process, which
was the case with an iterative server.
While a thread of a process is sleeping, the operating system could schedule the other threads
of this process to run.
With such a design, the waiting time of client requests, especially for those with a relatively
shorter processing time, could be significantly reduced.
15
Sample Implementation
• A server which return the reverse string sent by a client Client App 2
• Each client request will be handled by a “thread”
• Thread calls a function to reverse the string
Send a string
Client App 1
Thread()
Receives reverse string
Function
Thread()
Server App
16
Server Application
• Imports
• Functions
• Initialize the server
• Accept Clients and Start Threads
• Handle Client Request
thread.start_new_thread ( function, args[, kwargs] )
17
Client Program to Send Request and Display
• Client program to connect with the concurrent server and send
messages
18
Execution
• Observations
References
• https://codinghelp.io/creating-a-python-socket-server-with-multiple-
clients/
• https://www.geeksforgeeks.org/socket-programming-multi-
threading-python/
20
Paper Format
• 4 questions out of 5
• 1 – Basics of Wireless Technologies / BT /ZigBee
• 2 – Wifi – WLAN – Adhoc Networking
• 3 – Satellite Communication, Mobile Communication
• 4 – Basics of Network Programming in Python
• 5 – Iterative/Concurrent Servers in Python
21