|
import asyncio |
|
import os |
|
from multiprocessing import Process |
|
|
|
import aiohttp |
|
from channels.routing import ProtocolTypeRouter, get_default_application |
|
from django.conf import settings |
|
from django.core.asgi import get_asgi_application |
|
from django.http import HttpResponse |
|
from django.urls import path |
|
|
|
DEBUG = True |
|
ROOT_URLCONF = __name__ |
|
SECRET_KEY = "foo" |
|
INSTALLED_APPS = ["channels"] |
|
ASGI_APPLICATION = "app.channels_app" |
|
CHANNEL_LAYERS = { |
|
"default": {"BACKEND": "channels_redis.core.RedisChannelLayer"} |
|
} |
|
|
|
if not settings.configured: |
|
settings.configure(**{k: v for k, v in locals().items() if k.isupper()}) |
|
|
|
|
|
def get_request_id(): |
|
id = getattr(get_request_id, "_id", 1) |
|
get_request_id._id = id + 1 |
|
prefix = "channels" if is_channels else "django" |
|
return f"{prefix} {id:5}" |
|
|
|
|
|
async def sleep(request): |
|
request_id = get_request_id() |
|
print(request_id, "sleeping") |
|
await asyncio.sleep(0.01) |
|
print(request_id, "got response") |
|
return HttpResponse("slept well") |
|
|
|
|
|
async def internal_request(request): |
|
request_id = get_request_id() |
|
print(request_id, "fetching") |
|
url = "http://localhost:8000/hello" |
|
async with aiohttp.ClientSession() as session: |
|
async with session.get(url) as r: |
|
text = await r.text() |
|
print(request_id, f"got response: {text}") |
|
return HttpResponse(f"hello, {text}") |
|
|
|
|
|
async def external_request(request): |
|
request_id = get_request_id() |
|
print(request_id, "fetching") |
|
url = "http://localhost:8001" |
|
async with aiohttp.ClientSession() as session: |
|
async with session.get(url) as r: |
|
text = await r.text() |
|
print(request_id, f"got response: {text}") |
|
return HttpResponse(f"hello, {text}") |
|
|
|
|
|
def hello(request): |
|
return HttpResponse("world") |
|
|
|
|
|
urlpatterns = [ |
|
path("sleep", sleep), |
|
path("internal", internal_request), |
|
path("external", external_request), |
|
path("hello", hello), |
|
] |
|
|
|
channels_app = ProtocolTypeRouter({}) |
|
is_channels = os.environ.get("ASGI") == "channels" |
|
app = get_default_application() if is_channels else get_asgi_application() |
|
|
|
|
|
def external_server(): |
|
from aiohttp import web |
|
|
|
async def handle(request): |
|
return web.Response(text="external world") |
|
|
|
loop = asyncio.new_event_loop() |
|
asyncio.set_event_loop(loop) |
|
app = web.Application() |
|
app.add_routes([web.get("/", handle)]) |
|
web.run_app(app, port=8001) |
|
|
|
|
|
Process(target=external_server).start() |