Skip to main content

Global request for aiohttp server

Project description

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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiohttp_request-0.0.2.tar.gz (2.9 kB view details)

Uploaded Source

File details

Details for the file aiohttp_request-0.0.2.tar.gz.

File metadata

  • Download URL: aiohttp_request-0.0.2.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for aiohttp_request-0.0.2.tar.gz
Algorithm Hash digest
SHA256 e17701b995fccb036971bd82e7616e809ccb776bf61084c24f4f3bfbf54fdfbf
MD5 5c9799878916c88510fc8caf10bcb62e
BLAKE2b-256 eea4754f0aed1dccf724de3c593c441e9497ec2590ab8f523eba5ad188a307c7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page