Context 4 Healthcare Inc. 55 Shuman Blvd., Suite 650 Naperville, IL 60563 Phone: (800) 783-3378 Fax: (630) 654-1607. Subscribe to receive the latest company insights. 4: Examples or Explanations. This type of context clue uses examples to help the reader infer the meaning of a vocabulary word. Just like in any other situation, examples can be helpful illustrations as context clues. Now we transition to a summary of 3 general contexts: 4. Look Down at the Close Context “When we look down at the page, we seek to understand the words in their immediate context. The close context takes into account the divine inspiration and human character of the words written” (42–43).
Source code:Lib/contextlib.py
This module provides utilities for common tasks involving the withstatement. For more information see also Context Manager Types andWith Statement Context Managers.
29.6.1. Utilities¶
Functions and classes provided:
This function is a decorator that can be used to define a factoryfunction for with statement context managers, without needing tocreate a class or separate __enter__() and __exit__() methods.
A simple example (this is not recommended as a real way of generating HTML!): Money pro 1 9 – manage money like a program.
The function being decorated must return a generator-iterator whencalled. This iterator must yield exactly one value, which will be bound tothe targets in the with statement’s as clause, if any.
At the point where the generator yields, the block nested in the withstatement is executed. The generator is then resumed after the block is exited.If an unhandled exception occurs in the block, it is reraised inside thegenerator at the point where the yield occurred. Thus, you can use atry..except..finally statement to trapthe error (if any), or ensure that some cleanup takes place. If an exception istrapped merely in order to log it or to perform some action (rather than tosuppress it entirely), the generator must reraise that exception. Otherwise thegenerator context manager will indicate to the with statement thatthe exception has been handled, and execution will resume with the statementimmediately following the with statement.
contextmanager() uses ContextDecorator so the context managersit creates can be used as decorators as well as in with statements.When used as a decorator, a new generator instance is implicitly created oneach function call (this allows the otherwise “one-shot” context managerscreated by contextmanager() to meet the requirement that contextmanagers support multiple invocations in order to be used as decorators).
Changed in version 3.2: Use of ContextDecorator.
Return a context manager that closes thing upon completion of the block. Thisis basically equivalent to:
And lets you write code like this:
without needing to explicitly close page. Even if an error occurs,page.close() will be called when the with block is exited.
Return a context manager that suppresses any of the specified exceptionsif they occur in the body of a with statement and then resumes executionwith the first statement following the end of the with statement.
As with any other mechanism that completely suppresses exceptions, thiscontext manager should be used only to cover very specific errors wheresilently continuing with program execution is known to be the rightthing to do.
For example:
This code is equivalent to:
This context manager is reentrant.
New in version 3.4.
Context manager for temporarily redirecting sys.stdout toanother file or file-like object.
This tool adds flexibility to existing functions or classes whose outputis hardwired to stdout.
For example, the output of help() normally is sent to sys.stdout.You can capture that output in a string by redirecting the output to anio.StringIO object:
To send the output of help() to a file on disk, redirect the outputto a regular file:
To send the output of help() to sys.stderr:
Note that the global side effect on sys.stdout means that thiscontext manager is not suitable for use in library code and most threadedapplications. It also has no effect on the output of subprocesses.However, it is still a useful approach for many utility scripts.
This context manager is reentrant.
New in version 3.4.
A base class that enables a context manager to also be used as a decorator.
Context managers inheriting from ContextDecorator have to implement__enter__ and __exit__ as normal. __exit__ retains its optionalexception handling even when used as a decorator.
ContextDecorator is used by contextmanager(), so you get thisfunctionality automatically.
Example of ContextDecorator:
This change is just syntactic sugar for any construct of the following form:
ContextDecorator lets you instead write:
It makes it clear that the cm applies to the whole function, rather thanjust a piece of it (and saving an indentation level is nice, too).
Existing context managers that already have a base class can be extended byusing ContextDecorator as a mixin class:
Note
As the decorated function must be able to be called multiple times, theunderlying context manager must support use in multiple withstatements. If this is not the case, then the original construct with theexplicit with statement inside the function should be used.
A context manager that is designed to make it easy to programmaticallycombine other context managers and cleanup functions, especially thosethat are optional or otherwise driven by input data.
For example, a set of files may easily be handled in a single withstatement as follows:
Each instance maintains a stack of registered callbacks that are called inreverse order when the instance is closed (either explicitly or implicitlyat the end of a with statement). Note that callbacks are notinvoked implicitly when the context stack instance is garbage collected.
This stack model is used so that context managers that acquire theirresources in their __init__ method (such as file objects) can behandled correctly.
Since registered callbacks are invoked in the reverse order ofregistration, this ends up behaving as if multiple nested withstatements had been used with the registered set of callbacks. This evenextends to exception handling - if an inner callback suppresses or replacesan exception, then outer callbacks will be passed arguments based on thatupdated state.
This is a relatively low level API that takes care of the details ofcorrectly unwinding the stack of exit callbacks. It provides a suitablefoundation for higher level context managers that manipulate the exitstack in application specific ways.
New in version 3.3.
Enters a new context manager and adds its __exit__() method tothe callback stack. The return value is the result of the contextmanager’s own __enter__() method.
These context managers may suppress exceptions just as they normallywould if used directly as part of a with statement.
Adds a context manager’s __exit__() method to the callback stack.
As __enter__ is not invoked, this method can be used to coverpart of an __enter__() implementation with a context manager’s own__exit__() method.
If passed an object that is not a context manager, this method assumesit is a callback with the same signature as a context manager’s__exit__() method and adds it directly to the callback stack.
By returning true values, these callbacks can suppress exceptions thesame way context manager __exit__() methods can.
The passed in object is returned from the function, allowing thismethod to be used as a function decorator.
Accepts an arbitrary callback function and arguments and adds it tothe callback stack.
Unlike the other methods, callbacks added this way cannot suppressexceptions (as they are never passed the exception details).
The passed in callback is returned from the function, allowing thismethod to be used as a function decorator.
Transfers the callback stack to a fresh ExitStack instanceand returns it. No callbacks are invoked by this operation - instead,they will now be invoked when the new stack is closed (eitherexplicitly or implicitly at the end of a with statement).
For example, a group of files can be opened as an “all or nothing”operation as follows:
Immediately unwinds the callback stack, invoking callbacks in thereverse order of registration. For any context managers and exitcallbacks registered, the arguments passed in will indicate that noexception occurred.
29.6.2. Examples and Recipes¶
This section describes some examples and recipes for making effective use ofthe tools provided by contextlib.
29.6.2.1. Supporting a variable number of context managers¶
The primary use case for ExitStack is the one given in the classdocumentation: supporting a variable number of context managers and othercleanup operations in a single with statement. The variabilitymay come from the number of context managers needed being driven by userinput (such as opening a user specified collection of files), or fromsome of the context managers being optional:
As shown, ExitStack also makes it quite easy to use withstatements to manage arbitrary resources that don’t natively support thecontext management protocol. Tinertia 1 0 – action platformer game: without jump button.
29.6.2.2. Simplifying support for single optional context managers¶
In the specific case of a single optional context manager, ExitStackinstances can be used as a “do nothing” context manager, allowing a contextmanager to easily be omitted without affecting the overall structure ofthe source code:
29.6.2.3. Catching exceptions from __enter__ methods¶
It is occasionally desirable to catch exceptions from an __enter__method implementation, without inadvertently catching exceptions fromthe with statement body or the context manager’s __exit__method. By using ExitStack the steps in the context managementprotocol can be separated slightly in order to allow this:
Actually needing to do this is likely to indicate that the underlying APIshould be providing a direct resource management interface for use withtry/except/finally statements, but notall APIs are well designed in that regard. When a context manager is theonly resource management API provided, then ExitStack can make iteasier to handle various situations that can’t be handled directly in awith statement.
29.6.2.4. Cleaning up in an __enter__ implementation¶
As noted in the documentation of ExitStack.push(), thismethod can be useful in cleaning up an already allocated resource if latersteps in the __enter__() implementation fail.
Here’s an example of doing this for a context manager that accepts resourceacquisition and release functions, along with an optional validation function,and maps them to the context management protocol:
29.6.2.5. Replacing any use of try-finally and flag variables¶
A pattern you will sometimes see is a try-finally statement with a flagvariable to indicate whether or not the body of the finally clause shouldbe executed. In its simplest form (that can’t already be handled just byusing an except clause instead), it looks something like this:
As with any try statement based code, this can cause problems fordevelopment and review, because the setup code and the cleanup code can endup being separated by arbitrarily long sections of code.
ExitStack makes it possible to instead register a callback forexecution at the end of a with statement, and then later decide to skipexecuting that callback:
This allows the intended cleanup up behaviour to be made explicit up front,rather than requiring a separate flag variable.
If a particular application uses this pattern a lot, it can be simplifiedeven further by means of a small helper class:
If the resource cleanup isn’t already neatly bundled into a standalonefunction, then it is still possible to use the decorator form ofExitStack.callback() to declare the resource cleanup inadvance:
Due to the way the decorator protocol works, a callback functiondeclared this way cannot take any parameters. Instead, any resources tobe released must be accessed as closure variables.
29.6.2.6. Using a context manager as a function decorator¶
ContextDecorator makes it possible to use a context manager inboth an ordinary with statement and also as a function decorator.
For example, it is sometimes useful to wrap functions or groups of statementswith a logger that can track the time of entry and time of exit. Rather thanwriting both a function decorator and a context manager for the task,inheriting from ContextDecorator provides both capabilities in asingle definition:
Instances of this class can be used as both a context manager:
And also as a function decorator:
Note that there is one additional limitation when using context managersas function decorators: there’s no way to access the return value of__enter__(). If that value is needed, then it is still necessary to usean explicit with statement.
See also
- PEP 0343 - The “with” statement
- The specification, background, and examples for the Python withstatement.
29.6.3. Single use, reusable and reentrant context managers¶
![Contexts 3 4 4 = Contexts 3 4 4 =](https://upload.wikimedia.org/wikipedia/en/8/8d/The_Carbon_Literacy_Project_logo.jpg)
Most context managers are written in a way that means they can only beused effectively in a with statement once. These single usecontext managers must be created afresh each time they’re used -attempting to use them a second time will trigger an exception orotherwise not work correctly.
This common limitation means that it is generally advisable to createcontext managers directly in the header of the with statementwhere they are used (as shown in all of the usage examples above).
Files are an example of effectively single use context managers, sincethe first with statement will close the file, preventing anyfurther IO operations using that file object.
Contexts 3 4 4 X 4
Context managers created using contextmanager() are also single usecontext managers, and will complain about the underlying generator failingto yield if an attempt is made to use them a second time:
29.6.3.1. Reentrant context managers¶
Contexts 3 4 4 =
More sophisticated context managers may be “reentrant”. These contextmanagers can not only be used in multiple with statements,but may also be used inside a with statement that is alreadyusing the same context manager.
threading.RLock is an example of a reentrant context manager, as aresuppress() and redirect_stdout(). Here’s a very simple example ofreentrant use:
Real world examples of reentrancy are more likely to involve multiplefunctions calling each other and hence be far more complicated than thisexample.
Note also that being reentrant is not the same thing as being thread safe.redirect_stdout(), for example, is definitely not thread safe, as itmakes a global modification to the system state by binding sys.stdoutto a different stream.
29.6.3.2. Reusable context managers¶
Distinct from both single use and reentrant context managers are “reusable”context managers (or, to be completely explicit, “reusable, but notreentrant” context managers, since reentrant context managers are alsoreusable). These context managers support being used multiple times, butwill fail (or otherwise not work correctly) if the specific context managerinstance has already been used in a containing with statement.
threading.Lock is an example of a reusable, but not reentrant,context manager (for a reentrant lock, it is necessary to usethreading.RLock instead).
Another example of a reusable, but not reentrant, context manager isExitStack, as it invokes all currently registered callbackswhen leaving any with statement, regardless of where those callbackswere added:
As the output from the example shows, reusing a single stack object acrossmultiple with statements works correctly, but attempting to nest themwill cause the stack to be cleared at the end of the innermost withstatement, which is unlikely to be desirable behaviour.
Using separate ExitStack instances instead of reusing a singleinstance avoids that problem:
In computing, a context switch is the process of storing the state of a process or thread, so that it can be restored and resume execution at a later point. This allows multiple processes to share a single central processing unit (CPU), and is an essential feature of a multitasking operating system.
The precise meaning of the phrase “context switch” varies. In a multitasking context, it refers to the process of storing the system state for one task, so that task can be paused and another task resumed. A context switch can also occur as the result of an interrupt, such as when a task needs to access disk storage, freeing up CPU time for other tasks. Some operating systems also require a context switch to move between user mode and kernel mode tasks. The process of context switching can have a negative impact on system performance.[1]:28
Cost[edit]
Context switches are usually computationally intensive, and much of the design of operating systems is to optimize the use of context switches. Switching from one process to another requires a certain amount of time for doing the administration – saving and loading registers and memory maps, updating various tables and lists, etc. What is actually involved in a context switch depends on the architectures, operating systems, and the number of resources shared (threads that belong to the same process share many resources whether compared to unrelated non-cooperating processes. For example, in the Linux kernel, context switching involves switching registers, stack pointer (it's typical stack-pointer register), program counter, flushing the translation lookaside buffer (TLB) and loading the page table of the next process to run (unless the old process shares the memory with the new).[2][3] Furthermore, analogous context switching happens between user threads, notably green threads, and is often very lightweight, saving and restoring minimal context. In extreme cases, such as switching between goroutines in Go, a context switch is equivalent to a coroutine yield, which is only marginally more expensive than a subroutine call.
![Buckshot Buckshot](https://www.researchgate.net/profile/Margaret_Ackerman/publication/312103777/figure/download/fig1/AS:645835872288768@1530990679422/Affinity-of-human-IgG-subclasses-for-human-and-rhesus-FcgR-Equilibrium-binding-constants.png)
Switching cases[edit]
There are three potential triggers for a context switch:
Multitasking[edit]
Most commonly, within some scheduling scheme, one process must be switched out of the CPU so another process can run. This context switch can be triggered by the process making itself unrunnable, such as by waiting for an I/O or synchronization operation to complete. On a pre-emptive multitasking system, the scheduler may also switch out processes that are still runnable. To prevent other processes from being starved of CPU time, preemptive schedulers often configure a timer interrupt to fire when a process exceeds its time slice. This interrupt ensures that the scheduler will gain control to perform a context switch.
Interrupt handling[edit]
Modern architectures are interrupt driven. This means that if the CPU requests data from a disk, for example, it does not need to busy-wait until the read is over; it can issue the request (to the I/O device) and continue with some other task. When the read is over, the CPU can be interrupted (by a hardware in this case, which sends interrupt request to PIC) and presented with the read. For interrupts, a program called an interrupt handler is installed, and it is the interrupt handler that handles the interrupt from the disk.
When an interrupt occurs, the hardware automatically switches a part of the context (at least enough to allow the handler to return to the interrupted code). The handler may save additional context, depending on details of the particular hardware and software designs. Often only a minimal part of the context is changed in order to minimize the amount of time spent handling the interrupt. The kernel does not spawn or schedule a special process to handle interrupts, but instead the handler executes in the (often partial) context established at the beginning of interrupt handling. Once interrupt servicing is complete, the context in effect before the interrupt occurred is restored so that the interrupted process can resume execution in its proper state.
User and kernel mode switching[edit]
When the system transitions between user mode and kernel mode, a context switch is not necessary; a mode transition is not by itself a context switch. However, depending on the operating system, a context switch may also take place at this time.
Steps[edit]
In a switch, the state of the process currently executing must be saved somehow, so that when it is rescheduled, this state can be restored.
The process state includes all the registers that the process may be using, especially the program counter, plus any other operating system specific data that may be necessary. This is usually stored in a data structure called a process control block (PCB) or switchframe.
The PCB might be stored on a per-process stack in kernel memory (as opposed to the user-mode call stack), or there may be some specific operating system-defined data structure for this information. A handle to the PCB is added to a queue of processes that are ready to run, often called the ready queue.
Since the operating system has effectively suspended the execution of one process, it can then switch context by choosing a process from the ready queue and restoring its PCB. In doing so, the program counter from the PCB is loaded, and thus execution can continue in the chosen process. Process and thread priority can influence which process is chosen from the ready queue (i.e., it may be a priority queue).
Example[edit]
Considering a general arithmetic addition operation A = B+1. The instruction is stored in the instruction register and the program counter is incremented. A and B are read from memory and are stored in registers R1, R2 respectively. In this case, B+1 is calculated and written in R1 as the final answer. This operation only requires reads and writes that happen in a sequence/order and there are no function calls used, hence no context switch/wait takes place in this case.
However, certain special instructions require system calls that require context switch to wait/sleep processes. A system call handler is used for context switch to kernel mode. A display(data) function may require data from disk and a device driver in kernel mode, hence the display() function goes to sleep and waits on the 'read data' operation for the system call to wake it up when the “data” is received from the disk. i.e It waits for the function call to be released. After the “data” is received, the display(data) function is re-executed with the new value in order to prevent inconsistencies.
Performance[edit]
Context switching itself has a cost in performance, due to running the task scheduler, TLB flushes, and indirectly due to sharing the CPU cache between multiple tasks.[4] Switching between threads of a single process can be faster than between two separate processes, because threads share the same virtual memory maps, so a TLB flush is not necessary.[5]
Hardware vs. software[edit]
Context switching can be performed primarily by software or hardware. Some processors, like the Intel 80386 and its successors,[6] have hardware support for context switches, by making use of a special data segment designated the task state segment (TSS). A task switch can be explicitly triggered with a CALL or JMP instruction targeted at a TSS descriptor in the global descriptor table. It can occur implicitly when an interrupt or exception is triggered if there's a task gate in the interrupt descriptor table (IDT). When a task switch occurs the CPU can automatically load the new state from the TSS.
As with other tasks performed in hardware, one would expect this to be rather fast; however, mainstream operating systems, including Windows and Linux,[7] do not use this feature. This is mainly due to two reasons:
- Hardware context switching does not save all the registers (only general-purpose registers, not floating point registers — although the
TS
bit is automatically turned on in theCR0
control register, resulting in a fault when executing floating-point instructions and giving the OS the opportunity to save and restore the floating-point state as needed). - Associated performance issues, e.g., software context switching can be selective and store only those registers that need storing, whereas hardware context switching stores nearly all registers whether they are required or not.
See also[edit]
References[edit]
- ^Tanenbaum, Andrew S.; Bos, Herbert (March 20, 2014). Modern Operating Systems (4th ed.). Pearson. ISBN978-0133591620.
- ^IA-64 Linux Kernel: Design and Implementation, 4.7 Switching Address Spaces
- ^Operating Systems, 5.6 The Context Switch, p. 118
- ^Chuanpeng Li; Chen Ding; Kai Shen. 'Quantifying The Cost of Context Switch'(PDF).Cite journal requires
|journal=
(help) - ^Ulrich Drepper (9 October 2014). 'Memory part 3: Virtual Memory'. LWN.net.
- ^'Context Switch definition'. Linfo.org. Archived from the original on 2010-02-18. Retrieved 2013-09-08.
- ^Bovet, Daniel Pierre; Cesati, Marco (2006). Understanding the Linux Kernel, Third Edition. O'Reilly Media. p. 104. ISBN978-0-596-00565-8. Retrieved 2009-11-23.
External links[edit]
- Context Switching at OSDev.org
- Context Switch Definition by The Linux Information Project (LINFO)
- Context Switches from the Microsoft Developer Network (MSDN)
- General Architecture and Design -Interrupt Handling at FreeBSD.org
Retrieved from 'https://en.wikipedia.org/w/index.php?title=Context_switch&oldid=982575133'