0

I have a program that takes audio files and processes it.

@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))
        #this is a thread
        Recogniser(conv_path)
        print('Deleting MP3')
        os.remove(file_path)
        print('Deleting WAV')
        os.remove(conv_path)
        return render_template('upload.html')

I want my UI to be re-rendered after the files have been submitted to the thread for processing in the background. But it still keeps waiting.

Below is the code for my thread:



class Recogniser:

    def __init__(self, file):
        self.executor = ThreadPoolExecutor(5)
        self.file = file
        thread = threading.Thread(target=self.run(), daemon=True, args=file)
        thread.start()

    def run(self):
       #something
Kush Singh
  • 157
  • 3
  • 11

2 Answers2

0

Create the Recogniser class like this:

class Recogniser(threading.Thread):
    def __init__(self, file):
        self.file = file
        super().__init__()

    def run(self):
       #something
       pass

Then, start the thread like this:

thread_list = {}
@app.route('/', methods=['GET', 'POST'])
def hello_world():
    global thread_list
    # ... other logic ...
    thread_id = "generate some id"
    thread_list[thread_id] = Recogniser(file)
    thread_list[thread_id].start()
    # ... more logic ...
    return render_template('upload.html')

Note: The proper way would probably involve caching/saving thread IDs to the DB etc. But this should work for simpler apps.

aa20896
  • 16
  • 1
-1

You can use multiprocessing. Try like this:

from multiprocessing import Process

class Recogniser:

    def __init__(self, file):
        self.file = file
        thread = Process(target=self.run(), args=(file,))
        thread.start()

    def run(self):
       #something
S__
  • 404
  • 5
  • 13
  • The Webpage still waits for the process to finish – Kush Singh Jun 28 '20 at 20:49
  • Not sure. May be problem is how you call class. Try to use this process as method and not separate class. – S__ Jun 28 '20 at 21:02
  • the problem is that my request takes too long to process, i want to some how detach it from the main thread. – Kush Singh Jun 29 '20 at 10:56
  • so you have problem with request in run(self) or in hello_world() ? If in hello_world, just use it same like run(self) in separate process and check is alive or not in UI thread. – S__ Jul 02 '20 at 11:05