Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / node535.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="node536.html" />
<link rel="prev" href="module-SocketServer.html" />
<link rel="parent" href="module-SocketServer.html" />
<link rel="next" href="node536.html" />
<meta name='aesop' content='information' />
<title>11.16.1 Server Creation Notes</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="11.16 SocketServer "
href="module-SocketServer.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="11.16 SocketServer "
href="module-SocketServer.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="11.16.2 Server Objects"
href="node536.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-SocketServer.html">11.16 SocketServer </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-SocketServer.html">11.16 SocketServer </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node536.html">11.16.2 Server Objects</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H2><A NAME="SECTION00131610000000000000000">
11.16.1 Server Creation Notes</A>
</H2>
<P>
There are five classes in an inheritance diagram, four of which represent
synchronous servers of four types:
<P>
<div class="verbatim"><pre>
+------------+
| BaseServer |
+------------+
|
v
+-----------+ +------------------+
| TCPServer |-------&gt;| UnixStreamServer |
+-----------+ +------------------+
|
v
+-----------+ +--------------------+
| UDPServer |-------&gt;| UnixDatagramServer |
+-----------+ +--------------------+
</pre></div>
<P>
Note that <tt class="class">UnixDatagramServer</tt> derives from <tt class="class">UDPServer</tt>, not
from <tt class="class">UnixStreamServer</tt> - the only difference between an IP and a
Unix stream server is the address family, which is simply repeated in both
unix server classes.
<P>
Forking and threading versions of each type of server can be created using
the <tt class="class">ForkingMixIn</tt> and <tt class="class">ThreadingMixIn</tt> mix-in classes. For
instance, a threading UDP server class is created as follows:
<P>
<div class="verbatim"><pre>
class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass
</pre></div>
<P>
The mix-in class must come first, since it overrides a method defined in
<tt class="class">UDPServer</tt>. Setting the various member variables also changes the
behavior of the underlying server mechanism.
<P>
To implement a service, you must derive a class from
<tt class="class">BaseRequestHandler</tt> and redefine its <tt class="method">handle()</tt> method. You
can then run various versions of the service by combining one of the server
classes with your request handler class. The request handler class must be
different for datagram or stream services. This can be hidden by using the
handler subclasses <tt class="class">StreamRequestHandler</tt> or <tt class="class">DatagramRequestHandler</tt>.
<P>
Of course, you still have to use your head! For instance, it makes no sense
to use a forking server if the service contains state in memory that can be
modified by different requests, since the modifications in the child process
would never reach the initial state kept in the parent process and passed to
each child. In this case, you can use a threading server, but you will
probably have to use locks to protect the integrity of the shared data.
<P>
On the other hand, if you are building an HTTP server where all data is
stored externally (for instance, in the file system), a synchronous class
will essentially render the service "deaf" while one request is being
handled - which may be for a very long time if a client is slow to receive
all the data it has requested. Here a threading or forking server is
appropriate.
<P>
In some cases, it may be appropriate to process part of a request
synchronously, but to finish processing in a forked child depending on the
request data. This can be implemented by using a synchronous server and
doing an explicit fork in the request handler class <tt class="method">handle()</tt>
method.
<P>
Another approach to handling multiple simultaneous requests in an
environment that supports neither threads nor <tt class="function">fork()</tt> (or where
these are too expensive or inappropriate for the service) is to maintain an
explicit table of partially finished requests and to use <tt class="function">select()</tt>
to decide which request to work on next (or whether to handle a new incoming
request). This is particularly important for stream services where each
client can potentially be connected for a long time (if threads or
subprocesses cannot be used).
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="11.16 SocketServer "
href="module-SocketServer.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="11.16 SocketServer "
href="module-SocketServer.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="11.16.2 Server Objects"
href="node536.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-SocketServer.html">11.16 SocketServer </A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-SocketServer.html">11.16 SocketServer </A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="node536.html">11.16.2 Server Objects</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>