Create a hyperlink (or button) that executes a python script and then redirects when script completes

前端 未结 4 1811
死守一世寂寞
死守一世寂寞 2020-12-14 12:26

Alright, I managed to get it working how I want (although some/most might disagree with the method). I used Flask as recommended below. The part that might be considered \"w

4条回答
  •  -上瘾入骨i
    2020-12-14 13:05

    A simple way to tackle this problem would be to use a simple web framework like Flask to build the web part uf your system. In the request handler for your magic link, you would need to spawn your script and keep track of it. A simple way to see if your script is done and relay that to your user is to periodically send off an ajax request to check for completion.

    So for example the Flask website could look like:

    import threading
    import subprocess
    import uuid
    from flask import Flask
    from flask import render_template, url_for, abort, jsonify, request
    app = Flask(__name__)
    
    background_scripts = {}
    
    def run_script(id):
        subprocess.call(["/path/to/yourscript.py", "argument1", "argument2"])
        background_scripts[id] = True
    
    @app.route('/')
    def index():
        return render_template('index.html')
    
    @app.route('/generate')
    def generate():
        id = str(uuid.uuid4())
        background_scripts[id] = False
        threading.Thread(target=lambda: run_script(id)).start()
        return render_template('processing.html', id=id)
    
    @app.route('/is_done')
    def is_done():
        id = request.args.get('id', None)
        if id not in background_scripts:
            abort(404)
        return jsonify(done=background_scripts[id])
    

    And the index.html:

    click me
    

    And processing.html:

    
    
    
    
    
    
        Processing...
    
    

    This is all untested code at the moment, but I hope you get some idea on how to approach this problem. Also keep in mind that this will only work if you serve the page from one process, so if you set up Apache and mod_wsgi, make sure there is only one process in the process group.

    If you need a more complex solution, you might want to look at message queues and the like.

提交回复
热议问题