1. import threading
    
  2. 
    
  3. from django.http import FileResponse, HttpResponse
    
  4. from django.urls import path
    
  5. from django.views.decorators.csrf import csrf_exempt
    
  6. 
    
  7. 
    
  8. def hello(request):
    
  9.     name = request.GET.get("name") or "World"
    
  10.     return HttpResponse("Hello %s!" % name)
    
  11. 
    
  12. 
    
  13. def hello_meta(request):
    
  14.     return HttpResponse(
    
  15.         "From %s" % request.META.get("HTTP_REFERER") or "",
    
  16.         content_type=request.META.get("CONTENT_TYPE"),
    
  17.     )
    
  18. 
    
  19. 
    
  20. def sync_waiter(request):
    
  21.     with sync_waiter.lock:
    
  22.         sync_waiter.active_threads.add(threading.current_thread())
    
  23.     sync_waiter.barrier.wait(timeout=0.5)
    
  24.     return hello(request)
    
  25. 
    
  26. 
    
  27. @csrf_exempt
    
  28. def post_echo(request):
    
  29.     return HttpResponse(request.body)
    
  30. 
    
  31. 
    
  32. sync_waiter.active_threads = set()
    
  33. sync_waiter.lock = threading.Lock()
    
  34. sync_waiter.barrier = threading.Barrier(2)
    
  35. 
    
  36. 
    
  37. test_filename = __file__
    
  38. 
    
  39. 
    
  40. urlpatterns = [
    
  41.     path("", hello),
    
  42.     path("file/", lambda x: FileResponse(open(test_filename, "rb"))),
    
  43.     path("meta/", hello_meta),
    
  44.     path("post/", post_echo),
    
  45.     path("wait/", sync_waiter),
    
  46. ]