annotate MoinMoin/support/flup/server/threadedserver.py @ 5804:2e7f76626b58

update flup to repo tip (no release since 1.0.2 / 2.5y)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 01 Dec 2011 02:38:57 +0100
parents 6049883eb89e
children
rev   line source
4263
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
1 # Copyright (c) 2005 Allan Saddi <allan@saddi.com>
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
2 # All rights reserved.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
3 #
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
4 # Redistribution and use in source and binary forms, with or without
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
5 # modification, are permitted provided that the following conditions
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
6 # are met:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
7 # 1. Redistributions of source code must retain the above copyright
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
8 # notice, this list of conditions and the following disclaimer.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
9 # 2. Redistributions in binary form must reproduce the above copyright
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
10 # notice, this list of conditions and the following disclaimer in the
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
11 # documentation and/or other materials provided with the distribution.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
12 #
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
13 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
14 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
15 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
16 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
17 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
18 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
19 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
20 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
21 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
22 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
23 # SUCH DAMAGE.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
24 #
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
25 # $Id$
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
26
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
27 __author__ = 'Allan Saddi <allan@saddi.com>'
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
28 __version__ = '$Revision$'
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
29
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
30 import sys
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
31 import socket
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
32 import select
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
33 import signal
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
34 import errno
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
35
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
36 try:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
37 import fcntl
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
38 except ImportError:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
39 def setCloseOnExec(sock):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
40 pass
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
41 else:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
42 def setCloseOnExec(sock):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
43 fcntl.fcntl(sock.fileno(), fcntl.F_SETFD, fcntl.FD_CLOEXEC)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
44
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
45 from flup.server.threadpool import ThreadPool
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
46
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
47 __all__ = ['ThreadedServer']
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
48
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
49 class ThreadedServer(object):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
50 def __init__(self, jobClass=None, jobArgs=(), **kw):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
51 self._jobClass = jobClass
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
52 self._jobArgs = jobArgs
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
53
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
54 self._threadPool = ThreadPool(**kw)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
55
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
56 def run(self, sock, timeout=1.0):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
57 """
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
58 The main loop. Pass a socket that is ready to accept() client
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
59 connections. Return value will be True or False indiciating whether
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
60 or not the loop was exited due to SIGHUP.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
61 """
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
62 # Set up signal handlers.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
63 self._keepGoing = True
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
64 self._hupReceived = False
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
65
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
66 # Might need to revisit this?
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
67 if not sys.platform.startswith('win'):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
68 self._installSignalHandlers()
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
69
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
70 # Set close-on-exec
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
71 setCloseOnExec(sock)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
72
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
73 # Main loop.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
74 while self._keepGoing:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
75 try:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
76 r, w, e = select.select([sock], [], [], timeout)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
77 except select.error, e:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
78 if e[0] == errno.EINTR:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
79 continue
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
80 raise
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
81
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
82 if r:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
83 try:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
84 clientSock, addr = sock.accept()
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
85 except socket.error, e:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
86 if e[0] in (errno.EINTR, errno.EAGAIN):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
87 continue
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
88 raise
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
89
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
90 setCloseOnExec(clientSock)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
91
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
92 if not self._isClientAllowed(addr):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
93 clientSock.close()
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
94 continue
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
95
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
96 # Hand off to Connection.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
97 conn = self._jobClass(clientSock, addr, *self._jobArgs)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
98 if not self._threadPool.addJob(conn, allowQueuing=False):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
99 # No thread left, immediately close the socket to hopefully
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
100 # indicate to the web server that we're at our limit...
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
101 # and to prevent having too many opened (and useless)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
102 # files.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
103 clientSock.close()
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
104
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
105 self._mainloopPeriodic()
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
106
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
107 # Restore signal handlers.
5804
2e7f76626b58 update flup to repo tip (no release since 1.0.2 / 2.5y)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4902
diff changeset
108 if not sys.platform.startswith('win'):
2e7f76626b58 update flup to repo tip (no release since 1.0.2 / 2.5y)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4902
diff changeset
109 self._restoreSignalHandlers()
4263
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
110
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
111 # Return bool based on whether or not SIGHUP was received.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
112 return self._hupReceived
4902
6049883eb89e updated flup to 1.0.2+ (http://hg.saddi.com/flup-server changeset 100:af072c39193e)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4303
diff changeset
113
6049883eb89e updated flup to 1.0.2+ (http://hg.saddi.com/flup-server changeset 100:af072c39193e)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4303
diff changeset
114 def shutdown(self):
6049883eb89e updated flup to 1.0.2+ (http://hg.saddi.com/flup-server changeset 100:af072c39193e)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4303
diff changeset
115 """Wait for running threads to finish."""
6049883eb89e updated flup to 1.0.2+ (http://hg.saddi.com/flup-server changeset 100:af072c39193e)
Reimar Bauer <rb.proj AT googlemail DOT com>
parents: 4303
diff changeset
116 self._threadPool.shutdown()
4263
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
117
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
118 def _mainloopPeriodic(self):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
119 """
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
120 Called with just about each iteration of the main loop. Meant to
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
121 be overridden.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
122 """
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
123 pass
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
124
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
125 def _exit(self, reload=False):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
126 """
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
127 Protected convenience method for subclasses to force an exit. Not
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
128 really thread-safe, which is why it isn't public.
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
129 """
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
130 if self._keepGoing:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
131 self._keepGoing = False
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
132 self._hupReceived = reload
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
133
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
134 def _isClientAllowed(self, addr):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
135 """Override to provide access control."""
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
136 return True
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
137
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
138 # Signal handlers
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
139
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
140 def _hupHandler(self, signum, frame):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
141 self._hupReceived = True
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
142 self._keepGoing = False
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
143
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
144 def _intHandler(self, signum, frame):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
145 self._keepGoing = False
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
146
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
147 def _installSignalHandlers(self):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
148 supportedSignals = [signal.SIGINT, signal.SIGTERM]
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
149 if hasattr(signal, 'SIGHUP'):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
150 supportedSignals.append(signal.SIGHUP)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
151
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
152 self._oldSIGs = [(x,signal.getsignal(x)) for x in supportedSignals]
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
153
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
154 for sig in supportedSignals:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
155 if hasattr(signal, 'SIGHUP') and sig == signal.SIGHUP:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
156 signal.signal(sig, self._hupHandler)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
157 else:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
158 signal.signal(sig, self._intHandler)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
159
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
160 def _restoreSignalHandlers(self):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
161 for signum,handler in self._oldSIGs:
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
162 signal.signal(signum, handler)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
163
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
164 if __name__ == '__main__':
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
165 class TestJob(object):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
166 def __init__(self, sock, addr):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
167 self._sock = sock
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
168 self._addr = addr
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
169 def run(self):
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
170 print "Client connection opened from %s:%d" % self._addr
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
171 self._sock.send('Hello World!\n')
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
172 self._sock.setblocking(1)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
173 self._sock.recv(1)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
174 self._sock.close()
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
175 print "Client connection closed from %s:%d" % self._addr
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
176 sock = socket.socket()
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
177 sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
178 sock.bind(('', 8080))
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
179 sock.listen(socket.SOMAXCONN)
d40461b9ee6c Imported a current flup with single-thread server patch applied
Florian Krupicka <florian.krupicka@googlemail.com>
parents:
diff changeset
180 ThreadedServer(maxThreads=10, jobClass=TestJob).run(sock)