Misplaced Pages

Web Server Gateway Interface

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.

The Web Server Gateway Interface ( WSGI , pronounced whiskey or WIZ -ghee ) is a simple calling convention for web servers to forward requests to web applications or frameworks written in the Python programming language . The current version of WSGI, version 1.0.1, is specified in Python Enhancement Proposal (PEP) 3333.

#202797

66-449: WSGI was originally specified as PEP-333 in 2003. PEP-3333, published in 2010, updates the specification for Python 3 . In 2003, Python web frameworks were typically written against only CGI , FastCGI , mod_python , or some other custom API of a specific web server . To quote PEP 333: Python currently boasts a wide variety of web application frameworks, such as Zope, Quixote, Webware, SkunkWeb, PSO, and Twisted Web -- to name just

132-439: A std::unique_ptr . In addition, C++11's move semantics further reduce the extent to which reference counts need to be modified by removing the deep copy normally used when a function returns an object, as it allows for a simple copy of the pointer of said object. Apple's Cocoa and Cocoa Touch frameworks (and related frameworks, such as Core Foundation ) use manual reference counting, much like COM . Traditionally this

198-727: A "warnings" mode that highlighted the use of features that were removed in Python 3.0. Similarly, Python 2.7 coincided with and included features from Python 3.1, which was released on June 26, 2009. Parallel 2.x and 3.x releases then ceased, and Python 2.7 was the last release in the 2.x series. In November 2014, it was announced that Python 2.7 would be supported until 2020, but users were encouraged to move to Python 3 as soon as possible. Python 2.7 support ended on January 1, 2020, along with code freeze of 2.7 development branch. A final release, 2.7.18, occurred on April 20, 2020, and included fixes for critical bugs and release blockers. This marked

264-521: A WSGI network server is outside the scope of this article. Below is a sketch of how one would call a WSGI application and retrieve its HTTP status line, response headers, and response body, as Python objects. Details of how to construct the environ dict have been omitted. Numerous web frameworks support WSGI: Currently wrappers are available for FastCGI , CGI , SCGI , AJP (using flup), twisted.web , Apache (using mod_wsgi or mod_python ), Nginx (using ngx_http_uwsgi_module), Nginx Unit (using

330-549: A basic "literacy" in programming languages, similar to the basic English literacy and mathematics skills required by most employers. Python served a central role in this: because of its focus on clean syntax, it was already suitable, and CP4E's goals bore similarities to its predecessor, ABC. The project was funded by DARPA . As of 2007 , the CP4E project is inactive, and while Python attempts to be easily learnable and not too arcane in its syntax and semantics, outreach to non-programmers

396-431: A change to the development process itself, with a shift to a more transparent and community-backed process. Python 3.0, a major, backwards-incompatible release, was released on December 3, 2008 after a long period of testing. Many of its major features have also been backported to the backwards-compatible, though now-unsupported, Python 2.6 and 2.7. Releases of Python 3 include the 2to3 utility, which automates

462-405: A code block within a context manager (for example, acquiring a lock before the block of code is run and releasing the lock afterwards, or opening a file and then closing it), allowing resource acquisition is initialization (RAII)-like behavior and replacing a common try/finally idiom. Python 2.6 was released to coincide with Python 3.0, and included some features from that release, as well as

528-529: A copying nursery, observing that the majority of pointer mutations occur in young objects. This algorithm achieves throughput comparable with the fastest generational copying collectors with the low bounded pause times of reference counting. Perhaps the most obvious way to handle reference cycles is to design the system to avoid creating them. A system may explicitly forbid reference cycles; file systems with hard links often do this. Judicious use of "weak" (non-counted) references may also help avoid retain cycles;

594-542: A count of the number of references to it held by other objects. If an object's reference count reaches zero, the object has become inaccessible, and can be destroyed. When an object is destroyed, any objects referenced by that object also have their reference counts decreased. Because of this, removing a single reference can potentially lead to a large number of objects being freed. A common modification allows reference counting to be made incremental: instead of destroying an object as soon as its reference count becomes zero, it

660-403: A cycle of references brings them all down to zero. An enhanced version of this algorithm by Paz et al. is able to run concurrently with other operations and improve its efficiency by using the update coalescing method of Levanoni and Petrank. Although it is possible to augment simple reference counts in a variety of ways, often a better solution can be found by performing reference counting in

726-604: A cycle. PHP uses a reference counting mechanism for its internal variable management. Since PHP 5.3, it implements the algorithm from Bacon's above mentioned paper. PHP allows you to turn on and off the cycle collection with user-level functions. It also allows you to manually force the purging mechanism to be run. Python also uses reference counting and offers cycle detection as well (and can reclaim reference cycles). Like other low-level languages, Rust does not provide reference counting by default. Instead, any constructed type will be dropped when it falls out of scope. When

SECTION 10

#1732790261203

792-422: A few. This wide variety of choices can be a problem for new Python users, because generally speaking, their choice of web framework will limit their choice of usable web servers, and vice versa... By contrast, although Java has just as many web application frameworks available, Java's "servlet" API makes it possible for applications written with any Java web application framework to run in any web server that supports

858-426: A fundamentally different way. Here we describe some of the variants on reference counting and their benefits and drawbacks. In weighted reference counting, each reference is assigned a weight , and each object tracks not the number of references referring to it, but the total weight of the references referring to it. The initial reference to a newly created object has a large weight, such as 2 . Whenever this reference

924-459: A programmer needs to define the scope of a constructed type, they often use lifetimes. However, the language also offers various alternatives to complex forms of memory management. Reference counting functionality is provided by the Rc and Arc types, which are non-atomic and atomic respectively. For example, the type Rc<T> provides shared ownership of a value of type T , allocated on

990-449: A reference is created or destroyed can significantly impede performance. Not only do the operations take time, but they damage cache performance and can lead to pipeline bubbles . Even read-only operations like calculating the length of a list require a large number of reads and writes for reference updates with naive reference counting. One simple technique is for the compiler to combine a number of nearby reference updates into one. This

1056-399: A weight of 1, the reference has to "get more weight" by adding to the total weight and then adding this new weight to the reference, and then splitting it. An alternative in this situation is to create an indirection reference object, the initial reference to which is created with a large weight which can then be split. The property of not needing to access a reference count when a reference

1122-669: Is Python's principal author, and his continuing central role in deciding the direction of Python is reflected in the title given to him by the Python community, Benevolent Dictator for Life (BDFL) . (However, Van Rossum stepped down as leader on July 12, 2018. ). Python was named after the BBC TV show Monty Python's Flying Circus . Python 2.0 was released on October 16, 2000, with many major new features, such as list comprehensions , cycle-detecting garbage collector (in addition to reference counting ) and reference counting , for memory management and support for Unicode , along with

1188-413: Is added to a list of unreferenced objects, and periodically (or as needed) one or more items from this list are destroyed. Simple reference counts require frequent updates. Whenever a reference is destroyed or overwritten, the reference count of the object it references is decremented, and whenever one is created or copied, the reference count of the object it references is incremented. Reference counting

1254-469: Is also used in file systems and distributed systems, where full non-incremental tracing garbage collection is too time-consuming because of the size of the object graph and slow access speed. Microsoft's Component Object Model (COM) and WinRT makes pervasive use of reference counting. In fact, two of the three methods that all COM objects must provide (in the IUnknown interface) increment or decrement

1320-454: Is copied is particularly helpful when the object's reference count is expensive to access, for example because it is in another process, on disk, or even across a network. It can also help increase concurrency by avoiding many threads locking a reference count to increase it. Thus, weighted reference counting is most useful in parallel, multiprocess, database, or distributed applications. The primary problem with simple weighted reference counting

1386-412: Is copied, half of the weight goes to the new reference, and half of the weight stays with the old reference. Since the total weight does not change, the object's reference count does not need to be updated. Destroying a reference decrements the total weight by the weight of that reference. When the total weight becomes zero, all references have been destroyed. If an attempt is made to copy a reference with

SECTION 20

#1732790261203

1452-506: Is especially effective for references which are created and quickly destroyed. Care must be taken, however, to put the combined update at the right position so that a premature free can be avoided. The Deutsch-Bobrow method of reference counting capitalizes on the fact that most reference count updates are in fact generated by references stored in local variables. It ignores these references, only counting references in data structures, but before an object with reference count zero can be deleted,

1518-514: Is necessary to keep track of the reference's source. This means that two references are kept to the object: a direct one which is used for invocations; and an indirect one which forms part of a diffusion tree, such as in the Dijkstra–Scholten algorithm , which allows a garbage collector to identify dead objects. This approach prevents an object from being discarded prematurely. As a collection algorithm, reference counting tracks, for each object,

1584-433: Is no need to update the counter. This eliminates a large number of updates associated with short-lived references (such as the above list-length-counting example). However, if such a reference is copied into a data structure, then the deferred increment must be performed at that time. It is also critical to perform the deferred increment before the object's count drops to zero, to avoid a premature free. A dramatic decrease in

1650-490: Is not an active concern. In 2000, the Python core development team moved to BeOpen.com to form the BeOpen PythonLabs team. CNRI requested that a version 1.6 be released, summarizing Python's development up to the point at which the development team left CNRI. Consequently, the release schedules for 1.6 and 2.0 had a significant amount of overlap. Python 2.0 was the only release from BeOpen.com. After Python 2.0

1716-431: Is often helpful to think of the reference graph , which is a directed graph where the vertices are objects and there is an edge from an object A to an object B if A holds a reference to B. We also have a special vertex or vertices representing the local variables and references held by the runtime system, and no edges ever go to these nodes, although edges can go from them to other nodes. In this context,

1782-444: Is that destroying a reference still requires accessing the reference count, and if many references are destroyed, this can cause the same bottlenecks we seek to avoid. Some adaptations of weighted reference counting seek to avoid this by transferring weight from a dying reference to an active reference. Weighted reference counting was independently devised by Bevan and Watson & Watson in 1987. In indirect reference counting, it

1848-588: The Cocoa framework, for instance, recommends using "strong" references for parent-to-child relationships and "weak" references for child-to-parent relationships. Systems may also be designed to tolerate or correct the cycles they create in some way. Developers may design code to explicitly "tear down" the references in a data structure when it is no longer needed, though this has the cost of requiring them to manually track that data structure's lifetime. This technique can be automated by creating an "owner" object that does

1914-482: The end-of-life of Python 2. Python 3.0 (also called "Python 3000" or "Py3K") was released on December 3, 2008. It was designed to rectify fundamental design flaws in the language – the changes required could not be implemented while retaining full backwards compatibility with the 2.x series, which necessitated a new major version number. The guiding principle of Python 3 was: "reduce feature duplication by removing old ways of doing things". Python 3.0

1980-497: The functional programming languages SETL and Haskell . Python's syntax for this construct is very similar to Haskell's, apart from Haskell's preference for punctuation characters and Python's preference for alphabetic keywords. Python 2.0 also introduced a garbage collector able to collect reference cycles. Python 2.1 was close to Python 1.6.1, as well as Python 2.0. Its license was renamed Python Software Foundation License . All code, documentation and specifications added, from

2046-548: The Modula-3 inspired keyword arguments (which are also similar to Common Lisp 's keyword arguments) and built-in support for complex numbers . Also included is a basic form of data hiding by name mangling , though this is easily bypassed. During Van Rossum's stay at CNRI, he launched the Computer Programming for Everybody (CP4E) initiative, intending to make programming more accessible to more people, with

Web Server Gateway Interface - Misplaced Pages Continue

2112-481: The Objective-C runtime library in macOS Sierra . iOS has never supported a tracing garbage collector. Delphi is mostly not a garbage collected language, in that user-defined types must still be manually allocated and deallocated; however, it does provide automatic collection using reference counting for a few built-in types, such as strings, dynamic arrays , and interfaces, for ease of use and to simplify

2178-491: The Python 3.x platform using 2to3 . Edits to the Python 3.x code were discouraged for so long as the code needed to run on Python 2.x. This is no longer recommended; as of 2012 the preferred approach was to create a single code base that can run under both Python 2 and 3 using compatibility modules. Some of the major changes included for Python 3.0 were: Subsequent releases in the Python 3.x series have included additional, substantial new features; all ongoing development of

2244-601: The Python interpreter should be started, nor how the application object should be loaded or configured, and different frameworks and webservers achieve this in different ways. A WSGI middleware component is a Python callable that is itself a WSGI application, but may handle requests by delegating to other WSGI applications. These applications can themselves be WSGI middleware components. A middleware component can perform such functions as: A WSGI-compatible " Hello, World! " application written in Python : Where: A full example of

2310-596: The Python language module), and Microsoft IIS (using WFastCGI, isapi-wsgi, PyISAPIe, or an ASP gateway). Python 3 The programming language Python was conceived in the late 1980s, and its implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands as a successor to ABC capable of exception handling and interfacing with the Amoeba operating system . Van Rossum

2376-544: The choice-of-law clause was incompatible with the GNU General Public License . BeOpen, CNRI and the FSF negotiated a change to Python's free-software license that would make it GPL-compatible. Python 1.6.1 is essentially the same as Python 1.6, with a few minor bug fixes, and with the new GPL-compatible license. Python 2.0, released October 2000, introduced list comprehensions , a feature borrowed from

2442-505: The client is completely abstracted from whatever memory allocator the implementation of the COM object uses. As a typical example, a Visual Basic program using a COM object is agnostic towards whether that object was allocated (and must later be deallocated) by a C++ allocator or another Visual Basic component. C++ does not perform reference-counting by default, fulfilling its philosophy of not adding functionality that might incur overheads where

2508-516: The compiler (or runtime system ) knows that a particular object has only one reference (as most do in many systems), and that the reference is lost at the same time that a similar new object is created (as in the string append statement str ← str + "a" ), it can replace the operation with a mutation on the original object. Reference counting in naive form has three main disadvantages over the tracing garbage collection, both of which require additional mechanisms to ameliorate: In addition to these, if

2574-409: The data structure design. One simple solution is to periodically use a tracing garbage collector to reclaim cycles; since cycles typically constitute a relatively small amount of reclaimed space, the collector can be run much less often than with an ordinary tracing garbage collector. Bacon describes a cycle-collection algorithm for reference counting with similarities to tracing collectors, including

2640-678: The delayed reclamation may cause problems). Weighted reference counts are a good solution for garbage collecting a distributed system. Tracing garbage collection cycles are triggered too often if the set of live objects fills most of the available memory; it requires extra space to be efficient. Reference counting performance does not deteriorate as the total amount of free space decreases. Reference counts are also useful information to use as input to other runtime optimizations. For example, systems that depend heavily on immutable objects such as many functional programming languages can suffer an efficiency penalty due to frequent copies. However, if

2706-429: The details were intended to be more obvious in Python 3.0 than they were in Python 2.x. Python 3.0 broke backward compatibility, and much Python 2 code does not run unmodified on Python 3. Python's dynamic typing combined with the plans to change the semantics of certain methods of dictionaries, for example, made perfect mechanical translation from Python 2.x to Python 3.0 very difficult. A tool called " 2to3 " does

Web Server Gateway Interface - Misplaced Pages Continue

2772-689: The functional programming tools lambda , map , filter and reduce . Van Rossum stated that "Python acquired lambda, reduce(), filter() and map(), courtesy of a Lisp hacker who missed them and submitted working patches ". The last version released while Van Rossum was at CWI was Python 1.2. In 1995, Van Rossum continued his work on Python at the Corporation for National Research Initiatives (CNRI) in Reston , Virginia from where he released several versions. By version 1.4, Python had acquired several new features. Notable among these are

2838-750: The generic database functionality. It is up to the programmer to decide whether to use the built-in types; Delphi programmers have complete access to low-level memory management like in C/C++. So all potential cost of Delphi's reference counting can, if desired, be easily circumvented. Some of the reasons reference counting may have been preferred to other forms of garbage collection in Delphi include: The GObject object-oriented programming framework implements reference counting on its base types, including weak references . Reference incrementing and decrementing uses atomic operations for thread safety. A significant amount of

2904-476: The language is done in the 3.x series. Releases before numbered versions: Table notes: Reference counting In computer science , reference counting is a programming technique of storing the number of references , pointers , or handles to a resource, such as an object, a block of memory, disk space, and others. In garbage collection algorithms, reference counts may be used to deallocate objects that are no longer needed. The main advantage of

2970-485: The memory is allocated from a free list, reference counting suffers from poor locality. Reference counting alone cannot move objects to improve cache performance, so high performance collectors implement a tracing garbage collector as well. Most implementations (such as the ones in PHP and Objective-C) suffer from poor cache performance since they do not implement copying objects. When dealing with garbage collection schemes, it

3036-460: The module as "one of Python's major programming units". Python's exception model also resembles Modula-3's, with the addition of an else clause. In 1994 comp.lang.python , the primary discussion forum for Python, was formed, marking a milestone in the growth of Python's userbase and popularity. Python reached version 1.0 in January 1994. The major new features included in this release were

3102-547: The need to employ atomic operations during pointer updates in a concurrent setting, this solving reference counting issues in a concurrent setting. Therefore, update coalescing solves the third problem of naive reference counting (i.e., a costly overhead in a concurrent setting). Levanoni and Petrank presented an enhanced algorithm that may run concurrently with multithreaded applications employing only fine synchronization. Blackburn and McKinley's ulterior reference counting method in 2003 combines deferred reference counting with

3168-613: The overhead on counter updates was obtained by Levanoni and Petrank . They introduce the update coalescing method which coalesces many of the redundant reference count updates. Consider a pointer that in a given interval of the execution is updated several times. It first points to an object O1 , then to an object O2 , and so forth until at the end of the interval it points to some object On . A reference counting algorithm would typically execute rc(O1)-- , rc(O2)++ , rc(O2)-- , rc(O3)++ , rc(O3)-- , ..., rc(On)++ . But most of these updates are redundant. In order to have

3234-410: The parts of translation that can be done automatically. At this, 2to3 appeared to be fairly successful, though an early review noted that there were aspects of translation that such a tool would never be able to handle. Prior to the roll-out of Python 3, projects requiring compatibility with both the 2.x and 3.x series were recommended to have one source (for the 2.x series), and produce releases for

3300-474: The reference count properly evaluated at the end of the interval it is enough to perform rc(O1)-- and rc(On)++ . The rest of the updates are redundant. Levanoni and Petrank showed in 2001 how to use such update coalescing in a reference counting collector. When using update coalescing with an appropriate treatment of new objects, more than 99% of the counter updates are eliminated for typical Java benchmarks. Interestingly, update coalescing also eliminates

3366-560: The reference count. Much of the Windows Shell and many Windows applications (including MS Internet Explorer , MS Office , and countless third-party products) are built on COM, demonstrating the viability of reference counting in large-scale systems. One primary motivation for reference counting in COM is to enable interoperability across different programming languages and runtime systems. A client need only know how to invoke object methods in order to manage object life cycle; thus,

SECTION 50

#1732790261203

3432-655: The reference counting over tracing garbage collection is that objects are reclaimed as soon as they can no longer be referenced, and in an incremental fashion, without long pauses for collection cycles and with clearly defined lifetime of every object. In real-time applications or systems with limited memory, this is important to maintain responsiveness. Reference counting is also among the simplest forms of memory management to implement. It also allows for effective management of non-memory resources such as operating system objects, which are often much scarcer than memory (tracing garbage collection systems use finalizers for this, but

3498-414: The same theoretical time bounds. It is based on the observation that a cycle can only be isolated when a reference count is decremented to a nonzero value. All objects which this occurs on are put on a roots list, and then periodically the program searches through the objects reachable from the roots for cycles. It knows it has found a cycle that can be collected when decrementing all the reference counts on

3564-471: The servlet API. WSGI was thus created as an implementation-neutral interface between web servers and web applications or frameworks to promote common ground for portable web application development. The WSGI has two sides: Between the server and the application, there may be one or more WSGI middleware components , which implement both sides of the API, typically in Python code. WSGI does not specify how

3630-435: The simple reference count of an object is the in-degree of its vertex. Deleting a vertex is like collecting an object. It can only be done when the vertex has no incoming edges, so it does not affect the out-degree of any other vertices, but it can affect the in-degree of other vertices, causing their corresponding objects to be collected as well if their in-degree also becomes 0 as a result. The connected component containing

3696-461: The special vertex contains the objects that can't be collected, while other connected components of the graph only contain garbage. If a reference-counting garbage collection algorithm is implemented, then each of these garbage components must contain at least one cycle; otherwise, they would have been collected as soon as their reference count (i.e., the number of incoming edges) dropped to zero. Incrementing and decrementing reference counts every time

3762-402: The system must verify with a scan of the stack and registers that no other reference to it still exists. Another technique devised by Henry Baker involves deferred increments , in which references which are stored in local variables do not immediately increment the corresponding reference count, but instead defer this until it is necessary. If such a reference is destroyed quickly, then there

3828-551: The tearing-down when it is destroyed; for instance, a Graph object's destructor could delete the edges of its GraphNodes, breaking the reference cycles in the graph. Cycles may even be ignored in systems with short lives and a small amount of cyclic garbage, particularly when the system was developed using a methodology of avoiding cyclic data structures wherever possible, typically at the expense of efficiency. Computer scientists have also discovered ways to detect and collect reference cycles automatically, without requiring changes in

3894-563: The time of Python 2.1's alpha release on, is owned by the Python Software Foundation (PSF), a nonprofit organization formed in 2001, modeled after the Apache Software Foundation . The release included a change to the language specification to support nested scopes, like other statically scoped languages. (The feature was turned off by default, and not required, until Python 2.2.) Python 2.2

3960-422: The translation of Python 2 code to Python 3. In February 1991, Van Rossum published the code (labeled version 0.9.0) to alt.sources. Already present at this stage in development were classes with inheritance , exception handling, functions, and the core datatypes of list , dict , str and so on. Also in this initial release was a module system borrowed from Modula-3 ; Van Rossum describes

4026-685: The user has not explicitly requested it. Objects that are shared but not owned can be accessed via a reference, raw pointer, or iterator (a conceptual generalisation of pointers). However, by the same token, C++ provides native ways for users to opt-into such functionality: C++11 provides reference counted smart pointers , via the std::shared_ptr class, enabling automatic shared memory-management of dynamically allocated objects. Programmers can use this in conjunction with weak pointers (via std::weak_ptr ) to break cyclic dependencies. Objects that are dynamically allocated but not intended to be shared can have their lifetime automatically managed using

SECTION 60

#1732790261203

4092-575: The work in writing bindings to GObject from high-level languages lies in adapting GObject reference counting to work with the language's own memory management system. The Vala programming language uses GObject reference counting as its primary garbage collection system, along with copy-heavy string handling. Perl also uses reference counting, without any special handling of circular references, although (as in Cocoa and C++ above), Perl does support weak references, which allows programmers to avoid creating

4158-456: Was accomplished by the programmer manually sending retain and release messages to objects, but Automatic Reference Counting , a Clang compiler feature that automatically inserts these messages as needed, was added in iOS 5 and Mac OS X 10.7 . Mac OS X 10.5 introduced a tracing garbage collector as an alternative to reference counting, but it was deprecated in OS X 10.8 and removed from

4224-584: Was developed with the same philosophy as in prior versions. However, as Python had accumulated new and redundant ways to program the same task, Python 3.0 had an emphasis on removing duplicative constructs and modules, in keeping with the Zen of Python : "There should be one— and preferably only one —obvious way to do it". Nonetheless, Python 3.0 remained a multi-paradigm language . Coders could still follow object-oriented , structured , and functional programming paradigms, among others, but within such broad choices,

4290-512: Was released by BeOpen.com, Guido van Rossum and the other PythonLabs developers joined Digital Creations . The Python 1.6 release included a new CNRI license that was substantially longer than the CWI license that had been used for earlier releases. The new license included a clause stating that the license was governed by the laws of the State of Virginia . The Free Software Foundation argued that

4356-532: Was released in December 2001; a major innovation was the unification of Python's types (types written in C ) and classes (types written in Python) into one hierarchy. This single unification made Python's object model purely and consistently object oriented. Also added were generators which were inspired by Icon . Python 2.5 was released in September 2006 and introduced the with statement, which encloses

#202797