Skip to main content

aiohttp_request

info:

Global request for aiohttp server

https://travis-ci.org/hellysmile/aiohttp_request.svg?branch=master https://img.shields.io/pypi/v/aiohttp_request.svg https://codecov.io/gh/hellysmile/aiohttp_request/branch/master/graph/badge.svg

Installation

pip install aiohttp_request

Usage

import asyncio

from aiohttp import web
from aiohttp_request import ThreadContext, middleware_factory, grequest, get_request


def thread():
    assert grequest['sense'] == 42


async def task():
    # grequest is `lazy` version of request
    assert grequest['sense'] == 42

    loop = asyncio.get_event_loop()
    # works for threads as well with ThreadContext
    await loop.run_in_executor(None, ThreadContext(thread))


async def hello(request):
    # get_request is on-demand function to get current request
    assert get_request() is request

    request['sense'] = 42

    # asyncio.Task is supported
    await asyncio.ensure_future(task())

    return web.Response(text="Hello, world")


app = web.Application(middlewares=[middleware_factory()])
app.add_routes([web.get('/', hello)])
web.run_app(app)

Python 3.7+ is required, there is no way to support older python versions!!!

Notes

The library relies on PEP 567 and its asyncio support

aiohttp-request works nicely with threads via contextvars_executor , no ThreadContext is needed

import asyncio

from aiohttp import web
from aiohttp_request import middleware_factory, grequest
from contextvars_executor import ContextVarExecutor


def thread():
    assert grequest['sense'] == 42


async def hello(request):
    request['sense'] = 42

    loop = asyncio.get_event_loop()
    await loop.run_in_executor(None, thread)

    return web.Response(text="Hello, world")


loop = asyncio.get_event_loop()
loop.set_default_executor(ContextVarExecutor())
app = web.Application(middlewares=[middleware_factory()])
app.add_routes([web.get('/', hello)])
web.run_app(app)

Release files for aiohttp-request 0.0.2

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

Source distribution (sdist)

Source distribution for aiohttp-request 0.0.2
File Size Uploaded
aiohttp_request-0.0.2.tar.gz 2.9 kB Details

Release files / aiohttp_request-0.0.2.tar.gz

Download URL aiohttp_request-0.0.2.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
e17701b995fccb036971bd82e7616e809ccb776bf61084c24f4f3bfbf54fdfbf
BLAKE2b-256 checksum
How to use checksums
eea4754f0aed1dccf724de3c593c441e9497ec2590ab8f523eba5ad188a307c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.7

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

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