2021-06-30 23:37:10 +08:00
|
|
|
import threading
|
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
from django.http import FileResponse, HttpResponse
|
|
|
|
from django.urls import path
|
|
|
|
|
|
|
|
|
2019-06-17 22:27:04 +08:00
|
|
|
def hello(request):
|
|
|
|
name = request.GET.get('name') or 'World'
|
|
|
|
return HttpResponse('Hello %s!' % name)
|
|
|
|
|
|
|
|
|
|
|
|
def hello_meta(request):
|
|
|
|
return HttpResponse(
|
|
|
|
'From %s' % request.META.get('HTTP_REFERER') or '',
|
|
|
|
content_type=request.META.get('CONTENT_TYPE'),
|
|
|
|
)
|
2019-04-12 21:15:18 +08:00
|
|
|
|
|
|
|
|
2021-06-30 23:37:10 +08:00
|
|
|
def sync_waiter(request):
|
|
|
|
with sync_waiter.lock:
|
|
|
|
sync_waiter.active_threads.add(threading.current_thread())
|
|
|
|
sync_waiter.barrier.wait(timeout=0.5)
|
|
|
|
return hello(request)
|
|
|
|
|
|
|
|
|
|
|
|
sync_waiter.active_threads = set()
|
|
|
|
sync_waiter.lock = threading.Lock()
|
|
|
|
sync_waiter.barrier = threading.Barrier(2)
|
|
|
|
|
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
test_filename = __file__
|
|
|
|
|
|
|
|
|
|
|
|
urlpatterns = [
|
2019-06-17 22:27:04 +08:00
|
|
|
path('', hello),
|
2019-04-12 21:15:18 +08:00
|
|
|
path('file/', lambda x: FileResponse(open(test_filename, 'rb'))),
|
2019-06-17 22:27:04 +08:00
|
|
|
path('meta/', hello_meta),
|
2021-06-30 23:37:10 +08:00
|
|
|
path('wait/', sync_waiter),
|
2019-04-12 21:15:18 +08:00
|
|
|
]
|