Threaded Programming in Python

Slides:



Advertisements
Similar presentations
Chapter 15 Multithreading, Networks, and Client/Server Programming
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
Multithreaded Java COMP1681 / SE15 Introduction to Programming Fast Track Session 3.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Client Server Model and Software Design TCP/IP allows a programmer to establish communication between two application and to pass data back and forth.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 26 Client Server Interaction Communication across a computer network requires a pair of application programs to cooperate. One application on one.
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
1 Web Based Programming Section 8 James King 12 August 2003.
OS2014 PROJECT 2 Supplemental Information. Outline Sequence Diagram of Project 2 Kernel Modules Kernel Sockets Work Queues Synchronization.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Processes CSCI 4534 Chapter 4. Introduction Early computer systems allowed one program to be executed at a time –The program had complete control of the.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
The Client-Server Model And the Socket API. Client-Server (1) The datagram service does not require cooperation between the peer applications but such.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Venkita Subramonian, Christopher Gill, Ying Huang, Marc Sentany Department of Computer Science.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
 Process Concept  Process Scheduling  Operations on Processes  Cooperating Processes  Interprocess Communication  Communication in Client-Server.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
Tutorial 2: Homework 1 and Project 1
Multithreading The objectives of this chapter are:
Chapter 4 – Thread Concepts
Threads in Java Two ways to start a thread
Multithreading / Concurrency
Threaded Programming in Python
Processes and threads.
Process concept.
Process Management Process Concept Why only the global variables?
Chapter 3: Process Concept
PROCESS MANAGEMENT IN MACH
Multi Threading.
Boots Cassel Villanova University
Chapter 4 – Thread Concepts
Process Management Presented By Aditya Gupta Assistant Professor
Processes Overview: Process Concept Process Scheduling
Chapter 3: Process Concept
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Lecture 21 Concurrency Introduction
Chapter 3: Processes.
Client-Server Interaction
Multithreading Chapter 23.
Chapter 4: Processes Process Concept Process Scheduling
Lecture 2: Processes Part 1
Multithreading.
Operating System Concepts
Threads Chapter 4.
Multithreaded Programming
Chapter 3: Processes.
NETWORK PROGRAMMING CNET 441
CS333 Intro to Operating Systems
Outline Chapter 2 (cont) Chapter 3: Processes Virtual machines
Chapter 3: Process Concept
Chapter 4: Threads.
Multithreading The objectives of this chapter are:
Processes August 10, 2019 OS:Processes.
Chapter 3: Process Management
More concurrency issues
Presentation transcript:

Threaded Programming in Python CPE 401 / 601 Computer Network Systems Mehmet Hadi Gunes Adapted from Fundamentals of Python: From First Programs Through Data Structures

Objectives Describe what threads do and how they are manipulated in an application Code an algorithm to run as a thread Use conditions to solve a simple synchronization problem with threads Use IP addresses, ports, and sockets to create a simple client/server application on a network Decompose a server application with threads to handle client requests efficiently Restructure existing applications for deployment as client/server applications on a network

Threads In Python, a thread is an object like any other in that it can hold data, be run with methods, be stored in data structures, and be passed as parameters to methods A thread can also be executed as a process Before it can execute, a thread’s class must implement a run method During its lifetime, a thread can be in various states

Threads (continued)

Threads (continued) A thread remains inactive until start method runs Thread is placed in the ready queue Newly started thread’s run method is also activated A thread can lose access to the CPU: Time-out (process also known as time slicing) Sleep Block Wait Process of saving/restoring a thread’s state is called a context switch

Threads (continued) A thread’s run method is invoked automatically by start

Threads (continued) Most common way to create a thread is to define a class that extends the class threading.Thread

Sleeping Threads The function time.sleep puts a thread to sleep for the specified number of seconds

Sleeping Threads

Sleeping Threads

Handling Multiple Clients Concurrently To solve the problem of giving many clients timely access to the server, we assign task of handling the client’s request a client-handler thread

Server code Client code same as previous one

Multi-client Chat room Client code

Multi-client Chat room Server code

Multi-client Chat room Server code ChatRecord code

Simple Server Limitations Python Network Programming LinuxWorld, New York, January 20, 2004 Simple Server Limitations accept() [blocked] Client connection read() Server creates a new thread or forks a new process to handle each request Remote Client Process This diagram shows what needs to happen to allow parallel handling of concurrent requests. In the case of a forking server process, the forked child handles the request and the parent loops around to accept() another connection. In the case of a threading server the two threads coexist in the same process, and the process shares its CPU allocation between the new thread that processes the request and the original thread, which loops around to accept() another connection. The essential point is that the server no longer has to handle one connection completely before it can handle the next request. This isn't so important for UDP servers, where the request and response tend to be short and sweet. In the TCP world, however, a "request" is actually a connection that can be exceedingly long-lived – think of a Telnet or ssh session, for example. [blocked] write() Forked server process or thread runs independently Steve Holden, Holden Web LLC

Asynchronous Server Classes Python Network Programming LinuxWorld, New York, January 20, 2004 Asynchronous Server Classes A threading UDP server class is created as follows: class ThreadingUDPServer( ThreadingMixIn, UDPServer): pass The mix-in class must come first, since it overrides a method defined in UDPServer Steve Holden, Holden Web LLC

Producer, Consumer, and Synchronization Threads that interact by sharing data are said to have a producer/consumer relationship Example: an assembly line in a factory A producer must produce each item before a consumer consumes it Each item must be consumed before the producer produces the next item A consumer must consume each item just once We will simulate a producer/consumer relationship: Will share a single data cell with an integer

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization Threads sleep for random intervals

Producer, Consumer, and Synchronization Synchronization problems may arise: Consumer accesses the shared cell before the producer has written its first datum Producer then writes two consecutive data (1 and 2) before the consumer has accessed the cell again Consumer accesses data 2 twice Producer writes data 4 after consumer is finished Solution: synchronize producer/consumer threads States of shared cell: writeable or not writeable

Producer, Consumer, and Synchronization Solution (continued): Add two instance variables to SharedCell: a Boolean flag (_writeable) and an instance of threading.Condition A Condition maintains a lock on a resource

Producer, Consumer, and Synchronization

Producer, Consumer, and Synchronization Pattern for accessing a resource with a lock: Run acquire on the condition. While it’s not OK to do the work Run wait on the condition. Do the work with the resource. Run notify on the condition. Run release on the condition.

Producer, Consumer, and Synchronization

Summary Threads allow the work of a single program to be distributed among several computational processes States: born, ready, executing, sleeping, and waiting After a thread is started, it goes to the end of the ready queue to be scheduled for a turn in the CPU A thread may give up CPU when timed out, sleeps, waits on a condition, or finishes its run method When a thread wakes up, is timed out, or is notified that it can stop waiting, it returns to the rear of the ready queue Thread synchronization problems can occur when two or more threads share data A server can handle several clients concurrently by assigning each client request to a separate handler thread