echod.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/env python3
  2. # coding: utf-8
  3. import sys
  4. import asyncio
  5. import logging
  6. from prettyconf import config
  7. from echo import api
  8. debug = config("DEBUG", default=False, cast=config.boolean)
  9. log = logging.getLogger('echo')
  10. loga = logging.getLogger('asyncio')
  11. if debug:
  12. handler = logging.StreamHandler(sys.stdout)
  13. handler.setFormatter('[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] ')
  14. log.addHandler(handler)
  15. log.setLevel(logging.DEBUG)
  16. loga.addHandler(logging.StreamHandler(sys.stdout))
  17. loga.setLevel(logging.DEBUG)
  18. def main():
  19. loop = asyncio.get_event_loop()
  20. api_host = config('ECHO_API_HOST', default='127.0.0.1')
  21. api_port = config('ECHO_API_PORT', default=9876)
  22. server, handler, redis_pool = loop.run_until_complete(
  23. api.start(loop, api_host, api_port))
  24. try:
  25. loop.run_forever()
  26. except KeyboardInterrupt:
  27. pass
  28. finally:
  29. loop.run_until_complete(handler.finish_connections(1.0))
  30. loop.run_until_complete(redis_pool.clear())
  31. api.stop(loop)
  32. loop.close()
  33. sys.exit(0)
  34. if __name__ == '__main__':
  35. main()