2 # Example where a pool of http servers share a single listening socket
4 # On Windows this module depends on the ability to pickle a socket
5 # object so that the worker processes can inherit a copy of the server
6 # object. (We import `multiprocessing.reduction` to enable this pickling.)
8 # Not sure if we should synchronize access to `socket.accept()` method by
9 # using a process-shared lock -- does not seem to be necessary.
11 # Copyright (c) 2006-2008, R Oudkerk
12 # All rights reserved.
18 from multiprocessing
import Process
, current_process
, freeze_support
19 from BaseHTTPServer
import HTTPServer
20 from SimpleHTTPServer
import SimpleHTTPRequestHandler
22 if sys
.platform
== 'win32':
23 import multiprocessing
.reduction
# make sockets pickable/inheritable
26 def note(format
, *args
):
27 sys
.stderr
.write('[%s]\t%s\n' % (current_process().name
, format
%args
))
30 class RequestHandler(SimpleHTTPRequestHandler
):
31 # we override log_message() to show which process is handling the request
32 def log_message(self
, format
, *args
):
35 def serve_forever(server
):
36 note('starting server')
38 server
.serve_forever()
39 except KeyboardInterrupt:
43 def runpool(address
, number_of_processes
):
44 # create a single server object -- children will each inherit a copy
45 server
= HTTPServer(address
, RequestHandler
)
47 # create child processes to act as workers
48 for i
in range(number_of_processes
-1):
49 Process(target
=serve_forever
, args
=(server
,)).start()
51 # main process also acts as a worker
56 DIR
= os
.path
.join(os
.path
.dirname(__file__
), '..')
57 ADDRESS
= ('localhost', 8000)
58 NUMBER_OF_PROCESSES
= 4
60 print 'Serving at http://%s:%d using %d worker processes' % \
61 (ADDRESS
[0], ADDRESS
[1], NUMBER_OF_PROCESSES
)
62 print 'To exit press Ctrl-' + ['C', 'Break'][sys
.platform
=='win32']
65 runpool(ADDRESS
, NUMBER_OF_PROCESSES
)
68 if __name__
== '__main__':