Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hellysmile/aiohttp_request
Global request for aiohttp server
https://github.com/hellysmile/aiohttp_request
aiohttp asyncio import request
Last synced: 29 days ago
JSON representation
Global request for aiohttp server
- Host: GitHub
- URL: https://github.com/hellysmile/aiohttp_request
- Owner: hellysmile
- License: mit
- Created: 2018-06-30T00:09:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-12-06T12:02:03.000Z (about 1 month ago)
- Last Synced: 2024-12-06T13:38:25.121Z (about 1 month ago)
- Topics: aiohttp, asyncio, import, request
- Language: Python
- Homepage: https://pypi.org/project/aiohttp_request/
- Size: 39.1 KB
- Stars: 16
- Watchers: 3
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
aiohttp_request
===============:info: Global request for aiohttp server
.. image:: https://travis-ci.org/hellysmile/aiohttp_request.svg?branch=master
:target: https://travis-ci.org/hellysmile/aiohttp_request.. image:: https://img.shields.io/pypi/v/aiohttp_request.svg
:target: https://pypi.python.org/pypi/aiohttp_request.. image:: https://codecov.io/gh/hellysmile/aiohttp_request/branch/master/graph/badge.svg
:target: https://codecov.io/gh/hellysmile/aiohttp_requestInstallation
------------.. code-block:: shell
pip install aiohttp_request
Usage
-----.. code-block:: python
import asyncio
from aiohttp import web
from aiohttp_request import ThreadContext, middleware_factory, grequest, get_requestdef thread():
assert grequest['sense'] == 42async def task():
# grequest is `lazy` version of request
assert grequest['sense'] == 42loop = 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 requestrequest['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
.. code-block:: python
import asyncio
from aiohttp import web
from aiohttp_request import middleware_factory, grequest
from contextvars_executor import ContextVarExecutordef thread():
assert grequest['sense'] == 42async def hello(request):
request['sense'] = 42loop = 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)