Tasq Client Python
tasq-client-python is the official Python client library for the Tasq HTTP-based task queue server. The Tasq server (https://github.com/unixpickle/tasq) is a simple, lightweight system where tasks are pushed to a queue via HTTP endpoints, popped by workers, and then marked as complete. This client simplifies interaction with a running Tasq server, allowing Python applications to easily enqueue and process tasks. The library is currently at version 0.1.24 and has an irregular but active release cadence, tied to the development of the main Tasq server project.
Warnings
- gotcha The Tasq server's persistence mechanism (when using file-based saving) has limitations. Tasks pushed between the last save and a server restart will be lost. Tasks completed during this interval may reappear in the queue upon restart, requiring workers to be idempotent and handle already-completed tasks.
- gotcha The PyPI project page for `tasq-client-python` lacks a detailed description or direct link to documentation. Users must refer to the main `unixpickle/tasq` GitHub repository for comprehensive usage information, server setup, and protocol details.
- gotcha The `tasq-client-python` library is a client for the `tasq` server (https://github.com/unixpickle/tasq). This means you need a running `tasq` server instance for the client to function. Simply installing the Python client is not enough to have a functional task queue.
Install
-
pip install tasq-client-python
Imports
- TasqClient
from tasqclient import TasqClient
- TasqTask
from tasqclient import TasqTask
Quickstart
import asyncio
from tasqclient import TasqClient
import os
TASQ_SERVER_URL = os.environ.get('TASQ_SERVER_URL', 'http://localhost:8080')
async def main():
client = TasqClient(base_url=TASQ_SERVER_URL)
print(f"Connected to Tasq server at {TASQ_SERVER_URL}")
# Clear any existing tasks for a clean start
await client.clear()
print("Cleared existing tasks.")
# Push a task
task_content = "my-first-task-content"
pushed_task = await client.push(task_content)
print(f"Pushed task with ID: {pushed_task.id}, Content: {pushed_task.contents}")
# Pop a task
popped_task = await client.pop(timeout=0.1) # Short timeout for example
if popped_task:
print(f"Popped task with ID: {popped_task.id}, Content: {popped_task.contents}")
# Complete the task
await client.complete(popped_task.id)
print(f"Completed task with ID: {popped_task.id}")
else:
print("No task available to pop.")
# Get current counts
counts = await client.get_counts()
print(f"Current task counts: {counts}")
if __name__ == "__main__":
# Note: A Tasq server should be running at TASQ_SERVER_URL for this to work.
# You can run the server using `tasq -save-path state.json` in a separate terminal.
asyncio.run(main())