Bläddra i källkod

fix django server error (introduced in 0f50550) in coro-session

wangweimin 4 år sedan
förälder
incheckning
949bb3c748
2 ändrade filer med 19 tillägg och 20 borttagningar
  1. 18 15
      pywebio/platform/django.py
  2. 1 5
      test/14.django_multiple_session_impliment.py

+ 18 - 15
pywebio/platform/django.py

@@ -1,7 +1,7 @@
 import json
 import logging
 import threading
-from functools import partial
+import os
 
 from django.http import HttpResponse, HttpRequest
 
@@ -182,17 +182,20 @@ def start_server(applications, port=8080, host='localhost',
         path(r'<path:path>', serve, {'document_root': STATIC_PATH}),
     ]
 
-    app = get_wsgi_application()  # load app
-
-    has_coro_target = any(iscoroutinefunction(target) or isgeneratorfunction(target) for
-                          target in make_applications(applications).values())
-    if has_coro_target:
-        threading.Thread(target=run_event_loop, daemon=True).start()
-
-    # call_command('runserver', '%s:%d' % (host, port))
-    # or use below code to run web app
-    import tornado.wsgi
-    container = tornado.wsgi.WSGIContainer(app)
-    http_server = tornado.httpserver.HTTPServer(container)
-    http_server.listen(port, address=host)
-    tornado.ioloop.IOLoop.current().start()
+    use_tornado_wsgi = os.environ.get('PYWEBIO_DJANGO_WITH_TORNADO', True)
+    if use_tornado_wsgi:
+        app = get_wsgi_application()  # load app
+
+        import tornado.wsgi
+        container = tornado.wsgi.WSGIContainer(app)
+        http_server = tornado.httpserver.HTTPServer(container)
+        http_server.listen(port, address=host)
+        tornado.ioloop.IOLoop.current().start()
+    else:
+        from django.core.management import call_command
+        has_coro_target = any(iscoroutinefunction(target) or isgeneratorfunction(target) for
+                              target in make_applications(applications).values())
+        if has_coro_target:
+            threading.Thread(target=run_event_loop, daemon=True).start()
+
+        call_command('runserver', '%s:%d' % (host, port))

+ 1 - 5
test/14.django_multiple_session_impliment.py

@@ -7,7 +7,6 @@ import template
 import time
 import util
 from pywebio.input import *
-from pywebio.output import *
 from pywebio.utils import to_coroutine, run_as_function
 
 
@@ -52,9 +51,8 @@ def start_test_server():
     global urlpatterns
 
     pywebio.enable_debug()
-    import threading
     from functools import partial
-    from pywebio.platform.django import webio_view, run_event_loop
+    from pywebio.platform.django import webio_view
     from django.conf import settings
     from django.core.wsgi import get_wsgi_application
     from django.urls import path
@@ -100,8 +98,6 @@ def start_test_server():
 
     app = get_wsgi_application()  # load app
 
-    threading.Thread(target=run_event_loop, daemon=True).start()
-
     import tornado.wsgi
     container = tornado.wsgi.WSGIContainer(app)
     http_server = tornado.httpserver.HTTPServer(container)