Skip to content

Instantly share code, notes, and snippets.

@vsalvino
Last active December 1, 2021 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsalvino/10902f2c219bf32eb1c817cef1389223 to your computer and use it in GitHub Desktop.
Save vsalvino/10902f2c219bf32eb1c817cef1389223 to your computer and use it in GitHub Desktop.
Python threading in HTTP request
# NOTE: This requires threading support in your WSGI server.
# For uWSGI: https://uwsgi-docs.readthedocs.io/en/latest/WSGIquickstart.html#a-note-on-python-threads
import threading
# Long running task.
def run_thing(myuser):
...
# email user when done
return
# Django request.
def long_running(request):
t = threading.Thread(
target=run_thing,
kwargs={
"myuser": request.user,
},
daemon=True,
)
t.start()
return HttpResponse("You will recieve an email when done.", status=200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment