Tasq Client Python

0.1.24 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the TasqClient, push a task to the queue, pop it, and mark it as complete. It also shows how to clear the queue and get current task counts. Ensure a Tasq server is running and accessible at the specified URL (defaults to http://localhost:8080) for the example to function correctly.

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())

view raw JSON →