FastAPI queue tasks
Introduction
FastAPI Queue helps you add tasks to a queue and run them in parallel, supporting both asynchronous and synchronous task handlers.
How to use
1. Instantiate the Queue
First, create an instance of the Queue class by providing a Redis instance and configuration options like concurrency and max retry attempts:
# config/queue.py
from fastapi_queue_task import Queue
from redis.asyncio.utils import from_url
queue = Queue('main', redis, {'concurrency': QUEUE_MAX_CONCURRENCY, 'max_attempt': QUEUE_MAX_ATTEMP})
def queue_processing(task: Task):
print('TASK_DETAIL')
print('task name', task.get('name'))
print('task data', task.get('data'))
print('task attempt', task.get('attempt'))
def init_queue():
if env.IS_ALLOW_RUN_QUEUE:
queue.run(queue_processing)
# bootstrap/app.py
@asynccontextmanager
async def lifespan(app: FastAPI):
init_queue()
yield
app = FastAPI(title='API', lifespan=lifespan)
2. Add task to queue:
You can add a task to the queue by calling the add_to_queue method, passing the task name and the data to be processed:
# mail_service.py
from config import queue
await queue.add(name="TASK_NAME", data: Any = {})
async def track(channel_id: UUID, dto: TrackHotspotMediaViewDto):
return await queue.add(
TrackViewTaskEnum.POST,
{
'post_id': dto.post_id
},
)
3. Define a Task Handler
Define the task handler using the @task decorator. The handler will process the task asynchronously or synchronously based on its definition:
# tasks/track_view.py
from fastapi_queue_task import BaseTask, task
@task(TrackViewTaskEnum.POST)
class TrackViewPostTask(BaseTask):
def __init__(self, track_post_view_service: TrackPostViewService = Depends()):
self.track_post_view_service = track_post_view_service
async def handler(self, data: TrackPostViewDataType):
await self.track_post_view_service.handle_tracking(data)
In this example, the task TrackViewPostTask is added to the queue, and the handler method will process the task asynchronously.
Metadata
Release files for fastapi-queue-task 1.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_queue_task-1.0.4.tar.gz | 7.7 kB | Without provenance | Details for fastapi_queue_task-1.0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_queue_task-1.0.4-py3-none-any.whl | Python 3 | none | any | Details for fastapi_queue_task-1.0.4-py3-none-any.whl |
Total release size: 15.9 kB
Release files / fastapi_queue_task-1.0.4.tar.gz
| Download URL | fastapi_queue_task-1.0.4.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7e95057cc5dc1a318d569875fed06e4177e024afd341e548129950d1e575d85e
|
|
BLAKE2b-256 checksum How to use checksums |
3c14cecfec45b93d8ec8096fa6784c455feac6e3eae76cfebd0d75b63786dba7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Release files / fastapi_queue_task-1.0.4-py3-none-any.whl
| Download URL | fastapi_queue_task-1.0.4-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
38f95ea8d4c771f09aa36bf9d963338d26e17458352d7d7e6a149152ce40c484
|
|
BLAKE2b-256 checksum How to use checksums |
ea8afda7315026fd6621f0176ff597fdc2f640f741bb5fee3db7bb41a55a3cb2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.16
|
Security for fastapi-queue-task 1.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.