Why killing tasklets is good practice

Slides:



Advertisements
Similar presentations
Categories of I/O Devices
Advertisements

Gevent network library Denis Bilenko gevent.org. Problem statement from urllib2 import urlopen response = urlopen(' body = response.read()
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
1 CS345 Operating Systems Φροντιστήριο Άσκησης 1.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
CSE 451: Operating Systems Section 6 Project 2b; Midterm Review.
Concurrency CS 510: Programming Languages David Walker.
3.5 Interprocess Communication Many operating systems provide mechanisms for interprocess communication (IPC) –Processes must communicate with one another.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
3.5 Interprocess Communication
 2004 Deitel & Associates, Inc. All rights reserved. Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for Threads.
4.7.1 Thread Signal Delivery Two types of signals –Synchronous: Occur as a direct result of program execution Should be delivered to currently executing.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
CS-3103 & CS-502, Summer 2006 Programming Project #31 Programming Project #3 Web Server CS-3103 & CS-502 Operating Systems.
Fundamentals of Python: From First Programs Through Data Structures
Programming Network Servers Topic 6, Chapters 21, 22 Network Programming Kansas State University at Salina.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
1 Confidential Enterprise Solutions Group Process and Threads.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
1 (Worker Queues) cs What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.
Games Development 2 Concurrent Programming CO3301 Week 9.
Stackless Python in EVE Kristján Valur Jónsson CCP Games inc.
Threaded Programming in Python Adapted from Fundamentals of Python: From First Programs Through Data Structures CPE 401 / 601 Computer Network Systems.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
CSC Multiprocessor Programming, Spring, 2012 Chapter 7 – Cancellation & Shutdown Dr. Dale E. Parson, week 9-10.
Outline for Today Objectives –Finish discussion of Birrell –UNIX Signals –Eraser Administrative –Spider talk after class.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Introduction Contain two or more CPU share common memory and peripherals. Provide greater system throughput. Multiple processor executing simultaneous.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
1 5-High-Performance Embedded Systems using Concurrent Process (cont.)
ZOOKEEPER. CONTENTS ZooKeeper Overview ZooKeeper Basics ZooKeeper Architecture Getting Started with ZooKeeper.
EXCEPTIONS. Catching exceptions Whenever a runtime error occurs, it create an exception object. The program stops running at this point and Python prints.
Embedded Real-Time Systems Processing interrupts Lecturer Department University.
Tutorial 2: Homework 1 and Project 1
Chapter 4 – Thread Concepts
Introduction to threads
Threads Some of these slides were originally made by Dr. Roger deBry. They include text, figures, and information from this class’s textbook, Operating.
Threads in Java Two ways to start a thread
Chapter 4: Threads.
Threaded Programming in Python
Reactive Android Development
Scheduler activations
Chapter 4 – Thread Concepts
Multithreading Tutorial
Other Important Synchronization Primitives
Computer Engg, IIT(BHU)
5-High-Performance Embedded Systems using Concurrent Process (cont.)
Assignments A sample main program for Project 2 phase 2 is contained in: It reads.
Process Models, Creation and Termination
CS703 - Advanced Operating Systems
Multithreading.
Cancellation.
Threaded Programming in Python
Multithreading Tutorial
Multithreading Tutorial
Chapter 13: I/O Systems I/O Hardware Application I/O Interface
9. Threads SE2811 Software Component Design
Processes Creation and Threads
CSE 542: Operating Systems
Exceptions and networking
Presentation transcript:

Why killing tasklets is good practice Death to tasklets! Why killing tasklets is good practice

History Multiprocessing Multi-threading Master process spawns off multiple processes Waits for them to finish Multi-threading Within a process, threads are spawned „main thread“ waits for their results

Start Stop A typical problem Main „process“ starts a „server process“ Main process wants to stop server process Start Stop Get request Process request Finish request

How? Ask the server to quit Kill it with death! Message passing is nice Politeness is nice Good and tidy programing practice Kill it with death! Asynchronous (where does it get killed?) Impolite (will it clean up?) Impossible (what API is that?)

MEGADEATH Processes: Unix signals: Windows: SIGINT, can cause system calls to fail with an EINTERRUPTED error code. SIGKILL or SIGTERM just kills them outright Files are closed, memory is released. Buffers are not flushed Windows: TerminateProcess() kills a process outright Files, sockets, are closed, memory released, but buffers are not flushed

MEGADEATH Threads: pthreads Win32 Python pthread_cancel() on cancellation points. Win32 TerminateThread() kills the thread dead. Python Threading.Thread has no kill() method Python resources (references) would not be cleaned up by a hard kill Pthread_cancel() could be used to implement a ‚soft‘ kill but is not available on all platforms. CPython thread switching is synchronized (via GIL) so sending exceptions to threads is technically possible CPython devs haven‘t drunk the Svali yet.

MEGADEATH Killing processes/threads: If at all possible, it is messy (portability) C++ exception handling is usually not invoked Resources are released, but cleanup not executed Sockets are aborted, causing errors Locks are left un-released Buffers are left un-flushed Karma is left un-balanced

MEGADEATH Tasklets? Tasklet.kill() TaskletExit is BaseException Sends a TaskletExit exception to the tasklet Immediatelly awakes the target Optional „pending“ delivery mode of exception TaskletExit is BaseException Not caught by regular exception filters which catch Exception objects Normally causes the exception to bubble to the top Finally clauses and context managers run as expected

Microlife Killing tasklets is synchronous It is like sending a message to a blocking method Killing tasklets is not killing It is merely raising an exception It can even be caught, logged, handled, etc. Killing tasklet is not a system operation It invokes the language‘s native exception handling (unlike, e.g. C++)

Microlife Now we have an out-of-band way of sending messages to workers! Code written with proper try/finally scopes works as expected. Other apis and other exceptions can be used: Tasklet.kill(pending=True) # new feature Tasklet.throw(exc, val, tb, pending=False)

Microlife Examples: SocketServer.py def serve_forever(self, poll_interval=0.5):         """Handle one request at a time until shutdown.           Polls for shutdown every poll_interval seconds. Ignores         self.timeout. If you need to do periodic tasks, do them in         another thread.         """         self.__is_shut_down.clear()         try:             while not self.__shutdown_request:                 # XXX: Consider using another file descriptor or                 # connecting to the socket to wake this up instead of                 # polling. Polling reduces our responsiveness to a                 # shutdown request and wastes cpu at all other times.                 r, w, e = select.select([self], [], [], poll_interval)                 if self in r:                     self._handle_request_noblock()         finally:             self.__shutdown_request = False             self.__is_shut_down.set()

Microlife Examples: baseCorporation.py while True:             self.LogInfo("RunContactUpdates sleeping for", CONTACT_UPDATE_SLEEPTIME, "minutes")             blue.pyos.synchro.SleepWallclock(1000 * 60 * CONTACT_UPDATE_SLEEPTIME)             if not self.running:                 return …  

Dust UI UI code can be many tasklets doing things, e.g. Waiting for input. Complex UI state must be shut down e.g. When disconnection occurs UI tasklets register with the UI manager UI code carefully uses try:/finally: or context managers to release UI elements. UI manager can kill all registered tasklets, causing UI to be shut down gracefully.

Dust UI def UIDialogBoxWorker(): with UIManager.register_tasklet(): with UI.DialogueBox() as box: input = box.GetResult() # blocks here class UIManager(object): def OnClear(self): for worker in self.GetWorkers(): worker.kill()

Servitor Gated access to a service def StartProcess(self): self.servitor = sake.servitor.DirectServitor() def ServitorCall(self, function, args): """ Wrap the call. This can involve a servitor, for example. The purpose of this is to allow Crust to cancel any calls that are in progress. """ # Use the DirectServitor. This causes TaskletExit calls to result # in a stacklesslib.errors.CancelledError to be raised. return self.servitor.call(function, args) def Disconnect(self, *reasons): # We have decided to disconnect. Kill all calls currently in action self.servitor.kill_servitors(args=reasons) 

Servitor Clients is aware of CancelledError (or not): def GetThrused(): svc = app.GetService('ThrusingService') try: return svc.DoTheThrus('I am the bethrused. Thrus me!') except stacklesslib.errors.CancelledError: # handle this error here: return None

Servitor Server wraps its api: class ThrusingService(service): def StartService(self): self.servitor = servitor.SimpleServitor() def GetAPI(self): return servitor.WrapAPI(self) def CancelClients(self): self.servitor.cancel_clients() def DoTheThrus(self, subject): return sys.network.interact(self.target, subject)

Softkill Tasklet kill is a soft kill A gentle nudge of a rocking boat on a summer evening Requests a tasklet to politely stop what it is doing TaskletExit is a special exception that is silently ignored at the top level. Like a gentle rustle in the autumn leaves

Softkill What to be aware of: Every stackless switching function can raise an exception. Running under watchdog, every instruction can raise an exception. (see: atomic) Must be aware of this if writing low-level stackless code, such as when using channels. stacklesslib.locks classes are exception-aware So are uthread.locks and others in EVE, I believe.

Softkill Best practices: Use context managers to tear down resources Acquire locks, open files, etc. Know that blocking calls can raise exceptions But everyone knew that, right? Use „with stacklesslib.util.atomic():“ to guard critical stackless framework code. Because you just love to write your own synchronization primitives 

Do It! Never again write polling loops with exit request states Grow a moustache or plant an herb garden. Go forth and procreate.