当我尝试在django应用程序中运行代码时,我遇到了一些奇怪的错误
def main(request):
async def hello(request):
ws = web.WebSocketResponse()
await ws.prepare(request)
msg="this is awesome"
ws.send_str(msg)
# return HttpResponse("ok")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop = asyncio.get_event_loop()
loop.run_until_complete(hello(request))
loop.close()
当我运行这段代码时 我刚得到
Exception Value: 'AsgiRequest' object has no attribute 'loop'
这是问题的完整追溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/notifier/not/
Django Version: 2.0.7
Python Version: 3.6.4
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest',
'accounts',
'connect',
'drones',
'notifier',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
'channels',
'oauth2_provider',
'social_django',
'rest_framework_social_oauth2',
'drfpasswordless',
'taggit',
'taggit_serializer',
'elasticsearch']
Installed Middleware:
['corsheaders.middleware.CorsPostCsrfMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\exception.py" in inner
35. response = get_response(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\django\core\handlers\base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\madhumani\workspace\ven\drfc\notifier\views.py" in main
32. loop.run_until_complete(hello(request))
File "c:\users\madhumani\appdata\local\programs\python\python36-32\Lib\asyncio\base_events.py" in run_until_complete
467. return future.result()
File "C:\Users\madhumani\workspace\ven\drfc\notifier\views.py" in hello
23. await ws.prepare(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\aiohttp\web_ws.py" in prepare
106. protocol, writer = self._pre_start(request)
File "C:\Users\madhumani\workspace\ven\lib\site-packages\aiohttp\web_ws.py" in _pre_start
183. self._loop = request.loop
Exception Type: AttributeError at /notifier/not/
Exception Value: 'AsgiRequest' object has no attribute 'loop'