/Doc/includes/mp_webserver.py

http://unladen-swallow.googlecode.com/ · Python · 70 lines · 35 code · 18 blank · 17 comment · 5 complexity · a47bbb36143e72ce9efbf87d40cc50f5 MD5 · raw file

  1. #
  2. # Example where a pool of http servers share a single listening socket
  3. #
  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.)
  7. #
  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.
  10. #
  11. # Copyright (c) 2006-2008, R Oudkerk
  12. # All rights reserved.
  13. #
  14. import os
  15. import sys
  16. from multiprocessing import Process, current_process, freeze_support
  17. from BaseHTTPServer import HTTPServer
  18. from SimpleHTTPServer import SimpleHTTPRequestHandler
  19. if sys.platform == 'win32':
  20. import multiprocessing.reduction # make sockets pickable/inheritable
  21. def note(format, *args):
  22. sys.stderr.write('[%s]\t%s\n' % (current_process().name, format%args))
  23. class RequestHandler(SimpleHTTPRequestHandler):
  24. # we override log_message() to show which process is handling the request
  25. def log_message(self, format, *args):
  26. note(format, *args)
  27. def serve_forever(server):
  28. note('starting server')
  29. try:
  30. server.serve_forever()
  31. except KeyboardInterrupt:
  32. pass
  33. def runpool(address, number_of_processes):
  34. # create a single server object -- children will each inherit a copy
  35. server = HTTPServer(address, RequestHandler)
  36. # create child processes to act as workers
  37. for i in range(number_of_processes-1):
  38. Process(target=serve_forever, args=(server,)).start()
  39. # main process also acts as a worker
  40. serve_forever(server)
  41. def test():
  42. DIR = os.path.join(os.path.dirname(__file__), '..')
  43. ADDRESS = ('localhost', 8000)
  44. NUMBER_OF_PROCESSES = 4
  45. print 'Serving at http://%s:%d using %d worker processes' % \
  46. (ADDRESS[0], ADDRESS[1], NUMBER_OF_PROCESSES)
  47. print 'To exit press Ctrl-' + ['C', 'Break'][sys.platform=='win32']
  48. os.chdir(DIR)
  49. runpool(ADDRESS, NUMBER_OF_PROCESSES)
  50. if __name__ == '__main__':
  51. freeze_support()
  52. test()