Last active
December 1, 2021 19:06
-
-
Save vsalvino/10902f2c219bf32eb1c817cef1389223 to your computer and use it in GitHub Desktop.
Python threading in HTTP request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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