__init__.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. from waitress.server import create_server
  3. def serve(app, **kw):
  4. _server = kw.pop("_server", create_server) # test shim
  5. _quiet = kw.pop("_quiet", False) # test shim
  6. _profile = kw.pop("_profile", False) # test shim
  7. if not _quiet: # pragma: no cover
  8. # idempotent if logging has already been set up
  9. logging.basicConfig()
  10. server = _server(app, **kw)
  11. if not _quiet: # pragma: no cover
  12. server.print_listen("Serving on http://{}:{}")
  13. if _profile: # pragma: no cover
  14. profile("server.run()", globals(), locals(), (), False)
  15. else:
  16. server.run()
  17. def serve_paste(app, global_conf, **kw):
  18. serve(app, **kw)
  19. return 0
  20. def profile(cmd, globals, locals, sort_order, callers): # pragma: no cover
  21. # runs a command under the profiler and print profiling output at shutdown
  22. import os
  23. import profile
  24. import pstats
  25. import tempfile
  26. fd, fn = tempfile.mkstemp()
  27. try:
  28. profile.runctx(cmd, globals, locals, fn)
  29. stats = pstats.Stats(fn)
  30. stats.strip_dirs()
  31. # calls,time,cumulative and cumulative,calls,time are useful
  32. stats.sort_stats(*(sort_order or ("cumulative", "calls", "time")))
  33. if callers:
  34. stats.print_callers(0.3)
  35. else:
  36. stats.print_stats(0.3)
  37. finally:
  38. os.remove(fn)