{"id":24060,"library":"mode","title":"Mode","description":"Mode is an async service-based programming library built on top of asyncio. It provides Service, Worker, and BackgroundThreadService classes for composing asynchronous services. Current version is 4.4.0, requires Python ~=3.6. Release cadence is irregular.","status":"active","version":"4.4.0","language":"python","source_language":"en","source_url":"https://github.com/ask/mode","tags":["async","asyncio","services","worker","background-tasks"],"install":[{"cmd":"pip install mode","lang":"bash","label":"Install Mode via pip"}],"dependencies":[],"imports":[{"note":"Correct import path for the base Service class.","wrong":"","symbol":"Service","correct":"from mode import Service"},{"note":"Correct import for Worker loop.","wrong":"","symbol":"Worker","correct":"from mode import Worker"},{"note":"Used for services running in separate threads.","wrong":"","symbol":"BackgroundThreadService","correct":"from mode import BackgroundThreadService"}],"quickstart":{"code":"import asyncio\nfrom mode import Service\n\nclass MyService(Service):\n    async def run(self) -> None:\n        while not self.should_stop:\n            await asyncio.sleep(1)\n            print('Service running')\n\nasync def main():\n    service = MyService()\n    await service.start()\n    await asyncio.sleep(5)\n    await service.stop()\n\nif __name__ == '__main__':\n    asyncio.run(main())","lang":"python","description":"Simple example of creating and running a Mode Service."},"warnings":[{"fix":"Replace 'class MyWorker(Worker)' with 'class MyService(Service)' and adjust run() accordingly.","message":"The 'Worker' class is deprecated in favor of 'Service' for custom services. Use Service as base for new code.","severity":"deprecated","affected_versions":">=4.0.0"},{"fix":"Change run() to be an async def that uses await asyncio.sleep() or other async patterns instead of yield.","message":"In version 4.0.0, the 'run()' method signature changed: it no longer takes an 'async' generator approach but must be a coroutine. Old code using 'yield' in run() breaks.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Always use 'await service.start()' inside an async context.","message":"Service.start() must be awaited; common mistake is to call start() without await, leading to unstarted services.","severity":"gotcha","affected_versions":"all"},{"fix":"If you override __init__, always call 'super().__init__(**kwargs)' at the end of your __init__.","message":"Avoid overriding '__init__' in subclasses unless you call super().__init__() properly. Missing super() can break internal state.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Ensure all Mode services are created and started within the same event loop. Use 'asyncio.get_event_loop()' consistently or 'asyncio.run()' for top-level.","cause":"Mixing services created in different event loops, often due to threading or incorrect loop handling.","error":"RuntimeError: Task <Task pending ...> got Future <Future pending ...> attached to a different loop"},{"fix":"Change run() to use async/await pattern: 'async def run(self): await asyncio.sleep(...)' instead of 'while True: yield await ...'.","cause":"Using yield in Service.run() which is a coroutine, not an async generator.","error":"TypeError: 'async_generator' object is not iterable"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}