- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathmp_webserver.py
70 lines (52 loc) · 2.01 KB
/
mp_webserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
# Example where a pool of http servers share a single listening socket
#
# On Windows this module depends on the ability to pickle a socket
# object so that the worker processes can inherit a copy of the server
# object. (We import `multiprocessing.reduction` to enable this pickling.)
#
# Not sure if we should synchronize access to `socket.accept()` method by
# using a process-shared lock -- does not seem to be necessary.
#
# Copyright (c) 2006-2008, R Oudkerk
# All rights reserved.
#
importos
importsys
frommultiprocessingimportProcess, current_process, freeze_support
fromBaseHTTPServerimportHTTPServer
fromSimpleHTTPServerimportSimpleHTTPRequestHandler
ifsys.platform=='win32':
importmultiprocessing.reduction# make sockets pickable/inheritable
defnote(format, *args):
sys.stderr.write('[%s]\t%s\n'% (current_process().name, format%args))
classRequestHandler(SimpleHTTPRequestHandler):
# we override log_message() to show which process is handling the request
deflog_message(self, format, *args):
note(format, *args)
defserve_forever(server):
note('starting server')
try:
server.serve_forever()
exceptKeyboardInterrupt:
pass
defrunpool(address, number_of_processes):
# create a single server object -- children will each inherit a copy
server=HTTPServer(address, RequestHandler)
# create child processes to act as workers
foriinrange(number_of_processes-1):
Process(target=serve_forever, args=(server,)).start()
# main process also acts as a worker
serve_forever(server)
deftest():
DIR=os.path.join(os.path.dirname(__file__), '..')
ADDRESS= ('localhost', 8000)
NUMBER_OF_PROCESSES=4
print'Serving at http://%s:%d using %d worker processes'% \
(ADDRESS[0], ADDRESS[1], NUMBER_OF_PROCESSES)
print'To exit press Ctrl-'+ ['C', 'Break'][sys.platform=='win32']
os.chdir(DIR)
runpool(ADDRESS, NUMBER_OF_PROCESSES)
if__name__=='__main__':
freeze_support()
test()