Skip to content

Commit eedcd0d

Browse files
committed
added sleep route
1 parent fd96fe1 commit eedcd0d

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

gettingstarted/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
urlpatterns = [
2424
path("", hello.views.index, name="index"),
2525
path("db/", hello.views.db, name="db"),
26-
path("ao-custom/", hello.views.aocustom, name="aocustom"),
26+
path("ao-custom/", hello.views.ao_custom, name="ao_custom"),
27+
path("random-sleep/", hello.views.random_sleep, name="random_sleep"),
2728
# Uncomment this and the entry in `INSTALLED_APPS` if you wish to use the Django admin feature:
2829
# https://docs.djangoproject.com/en/5.2/ref/contrib/admin/
2930
# path("admin/", admin.site.urls),

hello/views.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import random
2+
import time
3+
14
from django.shortcuts import render
25

36
from .models import Greeting
@@ -8,7 +11,7 @@
811
def index(request):
912
return render(request, "index.html")
1013

11-
def aocustom(request):
14+
def ao_custom(request):
1215
# counts
1316
print("count#user.clicks=1")
1417
print("count#user.clicks.tagged=1 tag#user_id=2 tag#user_geo=earth")
@@ -41,3 +44,13 @@ def db(request):
4144
greetings = Greeting.objects.all()
4245

4346
return render(request, "db.html", {"greetings": greetings})
47+
48+
def random_sleep(request):
49+
# sleep for query parameter `sleepMs` milliseconds, or a random time between 0 and 3s if not specified
50+
try:
51+
sleep_s = int(request.GET["sleepMs"]) / 1000
52+
except (NameError, ValueError):
53+
sleep_s = random.uniform(0, 3.0)
54+
print(f"sleeping {sleep_s}s")
55+
time.sleep(sleep_s)
56+
return render(request, "index.html")

0 commit comments

Comments
 (0)