{"id":4334,"library":"zthreading","title":"Zthreading: Thread and Async Task Management","description":"Zthreading is a Python library offering a unified interface for managing both traditional Python threads and `asyncio` tasks. It provides wrapper classes for event broadcasting, task management, and decorators for common concurrency patterns. The library is actively maintained, with minor releases addressing bug fixes and new features every few months.","status":"active","version":"0.1.19","language":"en","source_language":"en","source_url":"https://github.com/LamaAni/zthreading.py","tags":["threading","asyncio","task-management","event-management","concurrency","decorators"],"install":[{"cmd":"pip install zthreading","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"ThreadManager","correct":"from zthreading.thread_manager import ThreadManager"},{"symbol":"AsyncioManager","correct":"from zthreading.asyncio_manager import AsyncioManager"},{"note":"Decorators are located in the `zthreading.decorators` submodule.","wrong":"from zthreading import as_task","symbol":"as_task","correct":"from zthreading.decorators import as_task"},{"note":"The `Event` class is part of the `zthreading.events` submodule since 0.1.11.","wrong":"from zthreading.event_manager import Event","symbol":"Event","correct":"from zthreading.events import Event"}],"quickstart":{"code":"import time\nfrom zthreading.thread_manager import ThreadManager\nfrom zthreading.decorators import as_task\n\ndef my_task(name):\n    print(f\"Task {name} started\")\n    time.sleep(1) # Simulate work\n    print(f\"Task {name} finished\")\n    return f\"Result for {name}\"\n\n# Initialize ThreadManager\nmanager = ThreadManager()\n\n# Add tasks\ntask1_handle = manager.add_task(my_task, args=(\"Task 1\",))\ntask2_handle = manager.add_task(my_task, args=(\"Task 2\",))\n\n# Use decorator for another task\n@as_task(manager=manager)\ndef decorated_task(name):\n    print(f\"Decorated task {name} started\")\n    time.sleep(0.5)\n    print(f\"Decorated task {name} finished\")\n    return f\"Decorated result for {name}\"\n\ndecorated_task_handle = decorated_task(\"Decorated Task 3\")\n\n# Wait for all tasks to complete\nmanager.wait_for_tasks()\n\nprint(f\"\\nTask 1 result: {task1_handle.get_result()}\")\nprint(f\"Task 2 result: {task2_handle.get_result()}\")\nprint(f\"Decorated task result: {decorated_task_handle.get_result()}\")\n\n# Ensure all threads are properly joined/terminated\nmanager.join_all()\n","lang":"python","description":"This quickstart demonstrates how to use `ThreadManager` to add and manage tasks, both directly and using the `as_task` decorator. It shows how to retrieve results and properly wait for tasks to complete, then clean up the manager."},"warnings":[{"fix":"Update event handler signatures to accept an `Event` object, and access event details (name, sender, args, kwargs) via its attributes (e.g., `evnt.name`, `evnt.args`).","message":"The signature for the `on_event` argument changed in version 0.1.11 from `on_event(name, *args, **kwargs)` to `on_event(evnt: Event)`. Event handlers now receive an `Event` object.","severity":"breaking","affected_versions":"<0.1.11"},{"fix":"Adjust the `predict` method implementation to accept the `event` object directly, similar to the `on_event` changes. The `Event` object encapsulates all relevant event data.","message":"The `predict` method signature for `wait_for_events` (and `wait_for`) was changed in version 0.1.13. It now takes `predict(handler, event)` instead of `predict(handler, name: str, *args, **kwargs)`.","severity":"breaking","affected_versions":"<0.1.13"},{"fix":"Upgrade `zthreading` to version 0.1.18 or newer to ensure compatibility with Python 3.10+ environments. The library now uses `threading.current_thread`.","message":"Older versions of `zthreading` (prior to 0.1.18) used `threading.currentThread` internally, which was deprecated in Python 3.10 and removed in Python 3.12. Running these older versions on Python 3.10+ will result in deprecation warnings or `AttributeError`.","severity":"gotcha","affected_versions":"<0.1.18"},{"fix":"Always explicitly wait for tasks using `manager.wait_for_tasks()` (for results) and/or `manager.join_all()` (for full cleanup of threads/tasks) before your application exits or when you no longer need the manager.","message":"When using `ThreadManager` or `AsyncioManager` with tasks, it's crucial to call `manager.wait_for_tasks()` or `manager.join_all()` to ensure all tasks complete and resources are cleaned up. Forgetting this can lead to unhandled tasks or processes remaining active.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}