I've been trying to run a file handling function in Python in a background thread using default threading library but the Chrome tab still waits for the api to finish the processing. I've tried threading & multiprocessing with no luck
Here is a snippet:
@app.route('/', methods=['GET', 'POST'])
def hello_world():
if request.method == 'GET':
return render_template('upload.html')
else:
file = request.files['file']
path = os.getcwd() + '\\tempFilesAudio\\'
if not os.path.exists(os.getcwd() + '\\' + 'tempFilesAudio'):
os.mkdir(os.getcwd() + '\\' + 'tempFilesAudio')
if not os.path.exists(os.getcwd() + '\\' + 'tempFilesTransciption'):
os.mkdir(os.getcwd() + '\\' + 'tempFilesTransciption')
file.save(path + secure_filename(file.filename))
file_path = path + file.filename
conv_path = convert(file_path)
print('converted:{}'.format(conv_path))
pr = r.Recogniser()
# this is a thread
Thread(target=pr.recognize(conv_path),daemon=True).start()
return redirect('/')
I understand you want to do this
from multiprocessing import Pool
# list of variables for Pool
args=[1, 2, 3, 4]
# We want to execute 4 threads at time
pool = Pool(4)
# Executing multithread process, we execute one function by item on the list
# This is like:
# functionName(args[0])
# functionName(args[1])
# functionName(args[2])
# functionName(args[3])
pool.map(functionName, args)
# We wait for the processes to finish
pool.close()
pool.join()
Is this correct?
If this is correct, also, you can use 'nohup' as Nano_developer told you, and execute your code without keep your session (This is the correct way), you need to do this from command line:
nohup python myFile.py