Stackless Python: programming the way Guido prevented it.

Slides:



Advertisements
Similar presentations
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Advertisements

Computer System Overview
Segmentation and Paging Considerations
ISBN Chapter 3 Describing Syntax and Semantics.
CS 355 – Programming Languages
Lightweight Remote Procedure Call Brian N. Bershad, Thomas E. Anderson, Edward D. Lazowska, and Henry M. Levy Presented by Alana Sweat.
A small part of what you should know about continuations, but were too afraid to ask Nick Benton.
Memory Management 2010.
Introduction to a Programming Environment
Describing Syntax and Semantics
03/29/2004CSCI 315 Operating Systems Design1 Page Replacement Algorithms (Virtual Memory)
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Layers and Views of a Computer System Operating System Services Program creation Program execution Access to I/O devices Controlled access to files System.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
Basics of Operating Systems March 4, 2001 Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard.
Process Description and Control. Process concepts n Definitions – replaces task, job – program in execution – entity that can be assigned to and executed.
Operating System A program that controls the execution of application programs An interface between applications and hardware 1.
High level & Low level language High level programming languages are more structured, are closer to spoken language and are more intuitive than low level.
Chapter 1 Computer System Overview Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles,
Memory Management Chapter 7.
C++ Beginner Tutorial: Functions IV Recursion. What is recursion? A property of function to be able to call itself… Get factorial of a given number: Factorial.
Threads Many software packages are multi-threaded Web browser: one thread display images, another thread retrieves data from the network Word processor:
Continuations and Stackless Python Where Do You Want To Jump Today?
Python Concurrency Threading, parallel and GIL adventures Chris McCafferty, SunGard Global Services.
A Revolutionary Programming Pattern that Will Clean up your Code : Coroutines in C++ David Sackstein ACCU 2015.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Computer Science and Software Engineering University of Wisconsin - Platteville 9. Recursion Yan Shi CS/SE 2630 Lecture Notes Partially adopted from C++
Lecture 12 Recursion part 1 Richard Gesick. Recursion A recursive method is a method that calls itself. A recursive method is capable of solving only.
Memory Management. Roadmap Basic requirements of Memory Management Memory Partitioning Basic blocks of memory management –Paging –Segmentation.
Stackless Python: programming the way Guido prevented it intended.
Operating Systems Lecture 7 OS Potpourri Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software.
Games Development 2 Concurrent Programming CO3301 Week 9.
VIRTUAL MEMORY By Thi Nguyen. Motivation  In early time, the main memory was not large enough to store and execute complex program as higher level languages.
Continuations and Stackless Python Where Do You Want To Jump Today?
By Teacher Asma Aleisa Year 1433 H.   Goals of memory management  To provide a convenient abstraction for programming.  To allocate scarce memory.
1 Combining Events and Threads for Scalable Network Services Peng Li and Steve Zdancewic University of Pennsylvania PLDI 2007, San Diego.
Chapter 11: Operating System Support Dr Mohamed Menacer Taibah University
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Lecture 4 Page 1 CS 111 Online Modularity and Virtualization CS 111 On-Line MS Program Operating Systems Peter Reiher.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Classical Control in Quantum Programs Dominique Unruh IAKS, Universität Karlsruhe Founded by the European Project ProSecCo IST
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Assembly Language Co-Routines
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Copyright © Curt Hill More on Operating Systems Continuation of Introduction.
PYTHON FOR HIGH PERFORMANCE COMPUTING. OUTLINE  Compiling for performance  Native ways for performance  Generator  Examples.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Contents 1.Overview 2.Multithreading Model 3.Thread Libraries 4.Threading Issues 5.Operating-system Example 2 OS Lab Sun Suk Kim.
Machine Language Computer languages cannot be directly interpreted by the computer – they are not in binary. All commands need to be translated into binary.
Python Joseph Eckstrom, Benjamin Moore, Willis Kornegay.
Tutorial 2: Homework 1 and Project 1
Functional Programming
Modeling Page Replacement Algorithms
William Stallings Computer Organization and Architecture
Call Stacks, Arguments and Returns
Control Flow.
Fundamentals of Programming
Modeling Page Replacement Algorithms
Operating Systems.
Binding Times Binding is an association between two things Examples:
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
Recursion Taken from notes by Dr. Neil Moore
Yan Shi CS/SE 2630 Lecture Notes
General Computer Science for Engineers CISC 106 Lecture 03
Introduction to Computer Science
Presentation transcript:

Stackless Python: programming the way Guido prevented it

Why Stackless is Cool Microthreads Generators Coroutines

Microthreads Very lightweight (can support thousands) Locks need not be OS resources Not for blocking I/O A comfortable model for people used to real threads

Generators A simple subcase of coroutines Often a more natural expression of an algorithm Replace a class / magic method with a function

Generator Example def fibonacci(): x, y = 0, 1 while 1: x, y = y, x+y suspend(y) Note: This example idealizes algorithmic clarity, but introduces a complexity on the calling side (fibonacci is a function object, while we want a generator object). This can be accommodated by having fibonacci() return a callable object that is then called to get successive results. Or fibonacci can stay as written, and the calling side can use a wrapper: fib = generator(fibonacci, ())

Coroutines Various ways to look at them Peer to peer subroutines Threads with voluntary swapping Generators on steroids (args in, args out) What’s so cool about them Both sides get to “drive” Often can replace a state machine with something more intuitive [1] [1] Especially where the state machine features complex state but relatively simple events (or few events per state).

Three Steps To Stacklessness Get Python data off the C stack Give each frame its own (Python) stackspace Get rid of interpreter recursions Result All frames are created equal Stack overflows become memory errors Pickling program state becomes conceivable

Getting rid of recursion is difficult Often there is “post” processing involved The C code (doing the recursing) may need its own “frame” Possible Approaches Tail optimized recursion Transformation to loop Either way, the “post” code needs to be separated from the “setup” code. Ironic Note: This is exactly the kind of pain we seek to relieve the Python programmer of!

Protecting against “illegal” transfers Can the extension (eg coroutines) provide the protection? Does the protection need to be in the Python core? Put another way: is thread / threading an acceptable model?

Language Support Can generators work naturally with __getitem__ ? Is any keyword support desirable (eg, suspend / resume)?