Skip to main content

Taskiq + AioHTTP

This project is used to create mocked application and request that you can use as a dependencies in your taskiq application.

It's useful because it runs all startup events of your application and everything that you might expect in your application's state is available inside of your tasks.

We suggest to use this library along with taskiq-python/aiohttp-deps, because it might be super handy to reuse same dependencies of your application in your tasks.

To add an integration, you need to call the function init in your broker's module.

import taskiq_aiohttp

broker = ...

taskiq_aiohttp.init(broker, "project.module:app")

How does it work?

It adds startup functions to the broker, so it imports your aiohttp application and creates a single worker-wide Request and Application objects that you can depend on.

THIS REQUEST IS NOT RELATED TO THE ACTUAL REQUESTS IN AioHTTP! This request won't have actual data about the request you were handling while sending a task.

Manual context updates

Sometimes it's required to update context manually. For example, for tests. If you need to add context in your broker by hand, please use function populate_context.

Imagine, you want to use InMemoryBroker for testing and your broker file looks like this:

broker = MyBroker()

if env == "pytest":
    broker = InMemoryBroker()

In this case your context won't be updated, because inmemory brokers cannot run as workers. To solve this issue, we have a populate context function. It's a bit complex and takes lots of parmeters. But here's a fixture that creates aiohttp test client and populates context of inmemory broker.

import asyncio
from typing import AsyncGenerator

import pytest
from aiohttp import web
from aiohttp.test_utils import BaseTestServer, TestClient, TestServer
from taskiq_aiohttp import populate_context


@pytest.fixture
async def test_client(
    app: web.Application,
) -> AsyncGenerator[TestClient, None]:
    """
    Create a test client.

    This function creates a TestServer
    and a test client for the application.

    Also this fixture populates context
    with needed variables.

    :param app: current application.
    :yield: ready to use client.
    """
    loop = asyncio.get_running_loop()
    server = TestServer(app)
    client = TestClient(server, loop=loop)

    await client.start_server()

    # This is important part.
    # Since InMemoryBroker doesn't
    # run in worker_process, we have to populate
    # broker's context manually.
    populate_context(
        broker=broker,
        server=server.runner.server,
        app=app,
        loop=None,
    )

    yield client

    await client.close()

Metadata

Release files for taskiq-aiohttp 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for taskiq-aiohttp 0.3.1
File Size Uploaded
taskiq_aiohttp-0.3.1.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for taskiq-aiohttp 0.3.1
File Interpreter ABI Platform
taskiq_aiohttp-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 10.0 kB

Release files / taskiq_aiohttp-0.3.1.tar.gz

Download URL taskiq_aiohttp-0.3.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
df62b137bc71fa7c6c470fca7e86b9afddbbadcc0bce7e16e2b9dc25ec45be9f
BLAKE2b-256 checksum
How to use checksums
a3bd5390c05baca78dc5529e712b3c4df5f3386050e1598976f24763ecd6defd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1016-azure

Release files / taskiq_aiohttp-0.3.1-py3-none-any.whl

Download URL taskiq_aiohttp-0.3.1-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3e282575bc6418c4a47ab2abae5430d410b1935e4e0ff8ba1fded10083eeb80d
BLAKE2b-256 checksum
How to use checksums
9c591fc715741f664ee6c28f5ba913c44d76f4bcba7445c55e030513ea023b4a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/6.2.0-1016-azure

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page