| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="node536.html" /> |
| 13 | <link rel="prev" href="module-SocketServer.html" /> |
| 14 | <link rel="parent" href="module-SocketServer.html" /> |
| 15 | <link rel="next" href="node536.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>11.16.1 Server Creation Notes</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="11.16 SocketServer " |
| 25 | href="module-SocketServer.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="11.16 SocketServer " |
| 28 | href="module-SocketServer.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="11.16.2 Server Objects" |
| 31 | href="node536.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="module-SocketServer.html">11.16 SocketServer </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-SocketServer.html">11.16 SocketServer </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="node536.html">11.16.2 Server Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION00131610000000000000000"> |
| 56 | 11.16.1 Server Creation Notes</A> |
| 57 | </H2> |
| 58 | |
| 59 | <P> |
| 60 | There are five classes in an inheritance diagram, four of which represent |
| 61 | synchronous servers of four types: |
| 62 | |
| 63 | <P> |
| 64 | <div class="verbatim"><pre> |
| 65 | +------------+ |
| 66 | | BaseServer | |
| 67 | +------------+ |
| 68 | | |
| 69 | v |
| 70 | +-----------+ +------------------+ |
| 71 | | TCPServer |------->| UnixStreamServer | |
| 72 | +-----------+ +------------------+ |
| 73 | | |
| 74 | v |
| 75 | +-----------+ +--------------------+ |
| 76 | | UDPServer |------->| UnixDatagramServer | |
| 77 | +-----------+ +--------------------+ |
| 78 | </pre></div> |
| 79 | |
| 80 | <P> |
| 81 | Note that <tt class="class">UnixDatagramServer</tt> derives from <tt class="class">UDPServer</tt>, not |
| 82 | from <tt class="class">UnixStreamServer</tt> - the only difference between an IP and a |
| 83 | Unix stream server is the address family, which is simply repeated in both |
| 84 | unix server classes. |
| 85 | |
| 86 | <P> |
| 87 | Forking and threading versions of each type of server can be created using |
| 88 | the <tt class="class">ForkingMixIn</tt> and <tt class="class">ThreadingMixIn</tt> mix-in classes. For |
| 89 | instance, a threading UDP server class is created as follows: |
| 90 | |
| 91 | <P> |
| 92 | <div class="verbatim"><pre> |
| 93 | class ThreadingUDPServer(ThreadingMixIn, UDPServer): pass |
| 94 | </pre></div> |
| 95 | |
| 96 | <P> |
| 97 | The mix-in class must come first, since it overrides a method defined in |
| 98 | <tt class="class">UDPServer</tt>. Setting the various member variables also changes the |
| 99 | behavior of the underlying server mechanism. |
| 100 | |
| 101 | <P> |
| 102 | To implement a service, you must derive a class from |
| 103 | <tt class="class">BaseRequestHandler</tt> and redefine its <tt class="method">handle()</tt> method. You |
| 104 | can then run various versions of the service by combining one of the server |
| 105 | classes with your request handler class. The request handler class must be |
| 106 | different for datagram or stream services. This can be hidden by using the |
| 107 | handler subclasses <tt class="class">StreamRequestHandler</tt> or <tt class="class">DatagramRequestHandler</tt>. |
| 108 | |
| 109 | <P> |
| 110 | Of course, you still have to use your head! For instance, it makes no sense |
| 111 | to use a forking server if the service contains state in memory that can be |
| 112 | modified by different requests, since the modifications in the child process |
| 113 | would never reach the initial state kept in the parent process and passed to |
| 114 | each child. In this case, you can use a threading server, but you will |
| 115 | probably have to use locks to protect the integrity of the shared data. |
| 116 | |
| 117 | <P> |
| 118 | On the other hand, if you are building an HTTP server where all data is |
| 119 | stored externally (for instance, in the file system), a synchronous class |
| 120 | will essentially render the service "deaf" while one request is being |
| 121 | handled - which may be for a very long time if a client is slow to receive |
| 122 | all the data it has requested. Here a threading or forking server is |
| 123 | appropriate. |
| 124 | |
| 125 | <P> |
| 126 | In some cases, it may be appropriate to process part of a request |
| 127 | synchronously, but to finish processing in a forked child depending on the |
| 128 | request data. This can be implemented by using a synchronous server and |
| 129 | doing an explicit fork in the request handler class <tt class="method">handle()</tt> |
| 130 | method. |
| 131 | |
| 132 | <P> |
| 133 | Another approach to handling multiple simultaneous requests in an |
| 134 | environment that supports neither threads nor <tt class="function">fork()</tt> (or where |
| 135 | these are too expensive or inappropriate for the service) is to maintain an |
| 136 | explicit table of partially finished requests and to use <tt class="function">select()</tt> |
| 137 | to decide which request to work on next (or whether to handle a new incoming |
| 138 | request). This is particularly important for stream services where each |
| 139 | client can potentially be connected for a long time (if threads or |
| 140 | subprocesses cannot be used). |
| 141 | |
| 142 | <P> |
| 143 | |
| 144 | <DIV CLASS="navigation"> |
| 145 | <div class='online-navigation'> |
| 146 | <p></p><hr /> |
| 147 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 148 | <tr> |
| 149 | <td class='online-navigation'><a rel="prev" title="11.16 SocketServer " |
| 150 | href="module-SocketServer.html"><img src='../icons/previous.png' |
| 151 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 152 | <td class='online-navigation'><a rel="parent" title="11.16 SocketServer " |
| 153 | href="module-SocketServer.html"><img src='../icons/up.png' |
| 154 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 155 | <td class='online-navigation'><a rel="next" title="11.16.2 Server Objects" |
| 156 | href="node536.html"><img src='../icons/next.png' |
| 157 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 158 | <td align="center" width="100%">Python Library Reference</td> |
| 159 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 160 | href="contents.html"><img src='../icons/contents.png' |
| 161 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 162 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 163 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 164 | <td class='online-navigation'><a rel="index" title="Index" |
| 165 | href="genindex.html"><img src='../icons/index.png' |
| 166 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 167 | </tr></table> |
| 168 | <div class='online-navigation'> |
| 169 | <b class="navlabel">Previous:</b> |
| 170 | <a class="sectref" rel="prev" href="module-SocketServer.html">11.16 SocketServer </A> |
| 171 | <b class="navlabel">Up:</b> |
| 172 | <a class="sectref" rel="parent" href="module-SocketServer.html">11.16 SocketServer </A> |
| 173 | <b class="navlabel">Next:</b> |
| 174 | <a class="sectref" rel="next" href="node536.html">11.16.2 Server Objects</A> |
| 175 | </div> |
| 176 | </div> |
| 177 | <hr /> |
| 178 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 179 | </DIV> |
| 180 | <!--End of Navigation Panel--> |
| 181 | <ADDRESS> |
| 182 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 183 | </ADDRESS> |
| 184 | </BODY> |
| 185 | </HTML> |