-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunAfterFinishWebpage.py
39 lines (26 loc) · 1.24 KB
/
RunAfterFinishWebpage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/python3
"""
This script is a webpage that fires off another Python script in the background, letting the user
move on without stopping background task.
This was a little project that was based on my need to fire-off a process that was not bound to a
web-user staying on the webpage.
Assumes this file is an 'endpoint' (ex. POST /run_after_finish_webpage.py) and that there is another
Python script (e.g. 'backgroundTask.py') in 'this' file's folder that will be run as a subprocess
even after 'this page' is done sending the end-of-request to the user.
Assumes, both on the very first line and also in the `args` creationg below, that the Python
executable is located at: /usr/bin/python3
"""
import subprocess
import os
python_file_to_run = "backgroundTask.py"
# Without this, anything that goes to the browser will just throw a 500
print("Content-type: text/html\r\n\r\n")
# Gather data from <form>, query-string, etc. for passing along via CLI
# - VITAL that these are sanitized!
some_args = "something here"
# Setup the executable
the_executabe = f'{os.getcwd()}{python_file_to_run}'
args = ['/usr/bin/python3', the_executable, some_arg]
# Start sub-process
variable_for_debugging = subprocess.Popen(args)
# Do other things here if/as needed