Python Concurrency Threading, parallel and GIL adventures Chris McCafferty, SunGard Global Services.

Slides:



Advertisements
Similar presentations
CS492B Analysis of Concurrent Programs Lock Basics Jaehyuk Huh Computer Science, KAIST.
Advertisements

MACHINE-INDEPENDENT VIRTUAL MEMORY MANAGEMENT FOR PAGED UNIPROCESSOR AND MULTIPROCESSOR ARCHITECTURES R. Rashid, A. Tevanian, M. Young, D. Golub, R. Baron,
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Computer Systems/Operating Systems - Class 8
Review: Chapters 1 – Chapter 1: OS is a layer between user and hardware to make life easier for user and use hardware efficiently Control program.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
Review: Process Communication Sequential Communication –Result of P1 becomes at termination the input to P2 –Tools: Redirect (>,>>) and Pipe (|) Concurrent.
Informationsteknologi Friday, November 16, 2007Computer Architecture I - Class 121 Today’s class Operating System Machine Level.
1/28/2004CSCI 315 Operating Systems Design1 Operating System Structures & Processes Notice: The slides for this lecture have been largely based on those.
Language Support for Lightweight transactions Tim Harris & Keir Fraser Presented by Narayanan Sundaram 04/28/2008.
Chapter 6 – Concurrent Programming Outline 6.1 Introduction 6.2Monitors 6.2.1Condition Variables 6.2.2Simple Resource Allocation with Monitors 6.2.3Monitor.
Concurrency, Threads, and Events Robbert van Renesse.
Programming Introduction November 9 Unit 7. What is Programming? Besides being a huge industry? Programming is the process used to write computer programs.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
PRASHANTHI NARAYAN NETTEM.
Fundamentals of Python: From First Programs Through Data Structures
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
CE Operating Systems Lecture 5 Processes. Overview of lecture In this lecture we will be looking at What is a process? Structure of a process Process.
Comparative Programming Languages hussein suleman uct csc304s 2003.
Parallel Programming Models Jihad El-Sana These slides are based on the book: Introduction to Parallel Computing, Blaise Barney, Lawrence Livermore National.
Understanding Operating Systems Fifth Edition Chapter 6 Concurrent Processes.
JIT in webkit. What’s JIT See time_compilation for more info. time_compilation.
Putting What We Learned Into Context – WSGI and Web Frameworks A290/A590, Fall /16/2014.
Threads, Thread management & Resource Management.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Chapter 4 Threads, SMP, and Microkernels Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
The HDF Group Multi-threading in HDF5: Paths Forward Current implementation - Future directions May 30-31, 2012HDF5 Workshop at PSI 1.
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:
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
Widespread Integration of Parallelism Steven Bogaerts.
Threads, SMP, and Microkernels Chapter 4. Process Resource ownership - process is allocated a virtual address space to hold the process image Scheduling/execution-
Games Development 2 Concurrent Programming CO3301 Week 9.
CSE 501N Fall ‘09 23: Advanced Multithreading: Synchronization and Thread-Safety December 1, 2009 Nick Leidenfrost.
CE Operating Systems Lecture 3 Overview of OS functions and structure.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Threading Eriq Muhammad Adams J
Kernel Locking Techniques by Robert Love presented by Scott Price.
Chapter 4 – Threads (Pgs 153 – 174). Threads  A "Basic Unit of CPU Utilization"  A technique that assists in performing parallel computation by setting.
1 Threads, SMP, and Microkernels Chapter Multithreading Operating system supports multiple threads of execution within a single process MS-DOS.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
NETW 3005 Threads and Data Sharing. Reading For this lecture, you should have read Chapter 4 (Sections 1-4). NETW3005 (Operating Systems) Lecture 03 -
Chapter 4 Software. Chapter 4: Software Generations of Languages Each computer is wired to perform certain operations in response to an instruction. An.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Processes & Threads Introduction to Operating Systems: Module 5.
Polytechnic University of Tirana Faculty of Information Technology Computer Engineering Department A MULTITHREADED SEARCH ENGINE AND TESTING OF MULTITHREADED.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
SMP Basics KeyStone Training Multicore Applications Literature Number: SPRPxxx 1.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
MULTIPROCESSING MODULE OF PYTHON. CPYTHON  CPython is the default, most-widely used implementation of the Python programming language.  CPython - single-threaded.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
JAVA TRAINING IN NOIDA. JAVA Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented and specifically.
A brief intro to: Parallelism, Threads, and Concurrency
multiprocessing and mpi4py Python for computational science
Faster Data Structures in Transactional Memory using Three Paths
Lecture 25 More Synchronized Data and Producer/Consumer Relationship
Lecture 5: GPU Compute Architecture
Lecture 5: GPU Compute Architecture for the last time
COP 4600 Operating Systems Fall 2010
Shared Memory Programming
Dr. Mustafa Cem Kasapbaşı
Concurrency: Mutual Exclusion and Process Synchronization
Prof. Leonardo Mostarda University of Camerino
- When you approach operating system concepts there might be several confusing terms that may look similar but in fact refer to different concepts:  multiprogramming, multiprocessing, multitasking,
Foundations and Definitions
Chapter 3: Process Management
Presentation transcript:

Python Concurrency Threading, parallel and GIL adventures Chris McCafferty, SunGard Global Services

Overview The free lunch is over – Herb Sutter Concurrency – traditionally challenging Threading The Global Interpreter Lock (GIL) Multiprocessing Parallel Processing Wrap-up – the Pythonic Way

Reminder - The Free Lunch Is Over

How do we get our free lunch back? Herb Sutter’s paper at: Clock speed increase is stalled but number of cores is increasing Parallel paths of execution will reduce time to perform computationally intensive tasks But multi-threaded development has typically been difficult and fraught with danger

Threading Use the threading module, not thread Offers usual helpers for making concurrency a bit less risky: Threads, Locks, Semaphores … Use logging, not print() Don’t start a thread in module import (bad) Careful importing from daemon threads

Traditional management view of Threads Baby pile of snakes, Justin Guyer

Managing Locks with ‘ with ’ With keyword is your friend (compare with the ‘with file’ idiom) import threading rlock = threading.RLock() with rlock: print "code that can only be executed while we acquire rlock" #lock is released at end of code block, regardless of exceptions

Atomic Operations in Python Some operations can be pre-empted by another thread This can lead to bad data or deadlocks Some languages offer constructs to help CPython has a set of atomic operations due to the operation of something called the GIL and the way the underlying C code is implemented This is a fortuitous implementation detail – ideally use RLocks to future-proof your code

CPython Atomic Operations reading or replacing a single instance attribute reading or replacing a single global variable fetching an item from a list modifying a list in place (e.g. adding an item using append) fetching an item from a dictionary modifying a dictionary in place (e.g. adding an item, or calling the clear method)

Example Processing Task

Threading Example See ThreadMaclaurin.py, compare with single- threaded SimpleMaclaurin.pyThreadMaclaurin.pySimpleMaclaurin.py Simple single-threaded example takes 4.522s 1 thread secs for iterations 2 threads secs for iterations 4 threads secs for iterations 6 threads secs for iterations 8 threads secs for iterations The time taken goes up not down with more than one thread?!?

The Global Interpreter Lock (GIL) Python is an interpreted language Only one thread can run in the interpreter at once Constant locking and signaling to see which thread gets the GIL next Detailed effect of this depends on your operating system Heavily affects CPU-bound problems

GIL – not a showstopper This is a known problem – brilliant minds are currently working on solutions Affects Ruby too and any sensible interpreted language Not noticeable on I/O-bound applications Lots of other solutions: Jython, multiprocessing, Stackless Python… Think in a Pythonic Way.

Threading with Jython Jython has many of the CPython modules Bytecode compiled, not fully interpreted, runs on the Java Virtual Machine 1 thread secs for iterations 2 threads secs for iterations 4 threads secs for iterations 6 threads secs for iterations 8 threads secs for iterations That’s more like it

Multiprocessing – no more GIL Snakes on a Plain, by Linda Frost

Multiprocessing Jython doesn’t have the multiprocessing module Each Python process has its own interpreter and GIL multiprocessing module makes managing processes and interprocess communication easy Use modules like pickle for passing payloads around Less worrying about shared memory and concurrency

Multiprocessing Example See MultiprocessMaclaurin.py for a simple example.MultiprocessMaclaurin.py Note use of a Queue to get the results back 1 thread secs for iterations 2 threads secs 4 threads secs 6 threads secs 8 threads secs

Multiprocessing - continued Remember there is an overhead associated with processes – don’t fork off thousands Full access to Cpython modules Be careful spawning processes from a script! Child process needs to be able to import the script or module containing the target function Can lead to recursive behaviour This can lead to processes being spawned until the machine crashes

Avoid multiprocessing recursion The ways to avoid recursive behaviour are: Have the target method in another module/script Protect the executed code with a test for __main__ : if __name__ == '__main__': p = multiprocessing.Process(target=worker, args=(i,)) p.start() Use a properly object-oriented structure in your code

Parallel Python Parallel Python module pp supports breaking up into tasks Detects number CPUs to decide process pool size for tasks No GIL effect Easily spread the load onto another machine running a pp process

Parallel Python Example In ParallelMaclaurin.py we stop caring about the number of processes or threadsParallelMaclaurin.py We operate at a higher level of abstraction Example breaks the problem into 64 tasks Running on an 8 core desktop: Time taken secs for iterations

Parallel Python for Big Data Job management and stats Symmetric or asymmetric computing Worry about decomposing and parallelising the task, not writing Locks and Semaphores Getting our free lunch back

Conclusions Python will support sensible threading constructs like any decent language Watch out for the GIL for CPU-bound tasks Switching to multiprocessing is easy Modules like pp support parallel processing and grid computing Lots of other options for I/O-bound problems: Stackless Python, Twisted… Many modules use threads sensibly behind the scenes Ideally, think Pythonicly – only move down the abstraction chain when you need to

Links Blog entry on much of this material ding-in-python/ ding-in-python/ David Beazley’s talks: beazly-in-search-of-the-perfect-global-interpreter-lock beazly-in-search-of-the-perfect-global-interpreter-lock global-interpreter-lock global-interpreter-lock Herb Sutter’s The Free Lunch Is Over:

Thank you Chris McCafferty Slides will be at: Contact me at: