Skip to content

Commit

Permalink
use from_thread.start_blocking_portal for Client
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Sep 9, 2024
1 parent 6d4735d commit 1ed8828
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 21 deletions.
12 changes: 4 additions & 8 deletions fasthtml/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from warnings import warn
from dateutil import parser as dtparse
from httpx import ASGITransport, AsyncClient
from anyio import from_thread

from .starlette import *

Expand Down Expand Up @@ -613,17 +614,12 @@ async def __call__(self, scope, receive, send) -> None:

# %% ../nbs/api/00_core.ipynb
class Client:
"An httpx ASGI client that doesn't require `async`"
"A simple httpx ASGI client that doesn't require `async`"
def __init__(self, app, url="http://testserver"):
self.cli = AsyncClient(transport=ASGITransport(app), base_url=url)

def _sync(self, method, url, **kwargs):
@threaded
def f():
async def _request(): return await self.cli.request(method, url, **kwargs)
return asyncio.run(_request())
r = f()
r.join()
return r.result
async def _request(): return await self.cli.request(method, url, **kwargs)
with from_thread.start_blocking_portal() as portal: return portal.call(_request)

for o in ('get', 'post', 'delete', 'put', 'patch', 'options'): setattr(Client, o, partialmethod(Client._sync, o))
46 changes: 33 additions & 13 deletions nbs/api/00_core.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"from warnings import warn\n",
"from dateutil import parser as dtparse\n",
"from httpx import ASGITransport, AsyncClient\n",
"from anyio import from_thread\n",
"\n",
"from fasthtml.starlette import *\n",
"\n",
Expand Down Expand Up @@ -128,7 +129,7 @@
{
"data": {
"text/plain": [
"datetime.datetime(2024, 9, 7, 14, 0)"
"datetime.datetime(2024, 9, 10, 14, 0)"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1430,28 +1431,47 @@
{
"cell_type": "code",
"execution_count": null,
"id": "41e0f473",
"id": "a3636f7c",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"class Client:\n",
" \"An httpx ASGI client that doesn't require `async`\"\n",
" \"A simple httpx ASGI client that doesn't require `async`\"\n",
" def __init__(self, app, url=\"http://testserver\"):\n",
" self.cli = AsyncClient(transport=ASGITransport(app), base_url=url)\n",
"\n",
" def _sync(self, method, url, **kwargs):\n",
" @threaded\n",
" def f():\n",
" async def _request(): return await self.cli.request(method, url, **kwargs)\n",
" return asyncio.run(_request())\n",
" r = f()\n",
" r.join()\n",
" return r.result\n",
" async def _request(): return await self.cli.request(method, url, **kwargs)\n",
" with from_thread.start_blocking_portal() as portal: return portal.call(_request)\n",
"\n",
"for o in ('get', 'post', 'delete', 'put', 'patch', 'options'): setattr(Client, o, partialmethod(Client._sync, o))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f2cf2f91",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'test'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"app = FastHTML(routes=[Route('/', lambda x: Response('test'))])\n",
"cli = Client(app)\n",
"\n",
"cli.get('/').text"
]
},
{
"cell_type": "markdown",
"id": "d7646aec",
Expand Down Expand Up @@ -2099,7 +2119,7 @@
{
"data": {
"text/plain": [
"'Cookie was set at time 16:51:39.109110'"
"'Cookie was set at time 00:25:24.236348'"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2129,13 +2149,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Set to 2024-09-07 16:51:39.146579\n"
"Set to 2024-09-10 00:25:24.272083\n"
]
},
{
"data": {
"text/plain": [
"'Session time: 2024-09-07 16:51:39.146579'"
"'Session time: 2024-09-10 00:25:24.272083'"
]
},
"execution_count": null,
Expand Down

0 comments on commit 1ed8828

Please sign in to comment.