/docs/source/entrypoint.rst

https://github.com/aiokitchen/aiomisc · ReStructuredText · 110 lines · 79 code · 31 blank · 0 comment · 0 complexity · 91a55bfe2ecf3f4b681b800f37a6bd22 MD5 · raw file

  1. entrypoint
  2. ==========
  3. In the generic case, the entrypoint helper creates an event loop and cancels already
  4. running coroutines on exit.
  5. .. code-block:: python
  6. :name: test_entrypoint_simple
  7. import asyncio
  8. import aiomisc
  9. async def main():
  10. await asyncio.sleep(1)
  11. with aiomisc.entrypoint() as loop:
  12. loop.run_until_complete(main())
  13. Complete example:
  14. .. code-block:: python
  15. :name: test_entrypoint_complex
  16. import asyncio
  17. import aiomisc
  18. import logging
  19. async def main():
  20. await asyncio.sleep(1)
  21. logging.info("Hello there")
  22. with aiomisc.entrypoint(
  23. pool_size=2,
  24. log_level='info',
  25. log_format='color', # default when "rich" absent
  26. log_buffer_size=1024, # default
  27. log_flush_interval=0.2, # default
  28. log_config=True, # default
  29. policy=asyncio.DefaultEventLoopPolicy(), # default
  30. debug=False, # default
  31. ) as loop:
  32. loop.run_until_complete(main())
  33. Running entrypoint from async code
  34. .. code-block:: python
  35. :name: test_entrypoint_async
  36. import asyncio
  37. import aiomisc
  38. import logging
  39. from aiomisc.service.periodic import PeriodicService
  40. log = logging.getLogger(__name__)
  41. class MyPeriodicService(PeriodicService):
  42. async def callback(self):
  43. log.info('Running periodic callback')
  44. # ...
  45. async def main():
  46. service = MyPeriodicService(interval=1, delay=0) # once per minute
  47. # returns an entrypoint instance because event-loop
  48. # already running and might be get via asyncio.get_event_loop()
  49. async with aiomisc.entrypoint(service) as ep:
  50. try:
  51. await asyncio.wait_for(ep.closing(), timeout=1)
  52. except asyncio.TimeoutError:
  53. pass
  54. asyncio.run(main())
  55. Configuration from environment
  56. ++++++++++++++++++++++++++++++
  57. Module support configuration from environment variables:
  58. * `AIOMISC_LOG_LEVEL` - default logging level
  59. * `AIOMISC_LOG_FORMAT` - default log format
  60. * `AIOMISC_LOG_CONFIG` - should logging be configured
  61. * `AIOMISC_LOG_FLUSH` - interval between logs flushing from buffer
  62. * `AIOMISC_LOG_BUFFERING` - should logging be buffered
  63. * `AIOMISC_LOG_BUFFER_SIZE` - maximum log buffer size
  64. * `AIOMISC_POOL_SIZE` - thread pool size
  65. ``run()`` shortcut
  66. ==================
  67. ``aiomisc.run()`` - it's the short way to create and destroy
  68. ``aiomisc.entrypoint``. It's very similar to ``asyncio.run()``
  69. but handle ``Service``'s and other ``entrypoint``'s kwargs.
  70. .. code-block:: python
  71. :name: test_ep_run_simple
  72. import asyncio
  73. import aiomisc
  74. async def main():
  75. loop = asyncio.get_event_loop()
  76. now = loop.time()
  77. await asyncio.sleep(0.1)
  78. assert now < loop.time()
  79. aiomisc.run(main())