Created
November 13, 2021 21:39
-
-
Save subdavis/68e88a38553ef6dcdd1fb9f05097ba42 to your computer and use it in GitHub Desktop.
a python function that will spin up a flask server and wait for a POST request, then return the response synchronously
This file contains hidden or 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
import logging | |
import sys | |
from multiprocessing import Process, Queue | |
from flask import Flask, request | |
global value | |
def _shutdown_server(): | |
func = request.environ.get('werkzeug.server.shutdown') | |
if func is None: | |
raise RuntimeError('Not running with the Werkzeug Server') | |
func() | |
def waitfor() -> str: | |
app = Flask(__name__) | |
log = logging.getLogger('werkzeug') | |
log.setLevel(logging.ERROR) | |
log.disabled = True | |
cli = sys.modules['flask.cli'] | |
cli.show_server_banner = lambda *x: None | |
server = Process(target=app.run, kwargs={'port': 8189, 'ssl_context': 'adhoc'}) | |
q = Queue() | |
@app.route('/', methods=['POST']) | |
def index(): | |
global value | |
q.put(request.form.get('value')) | |
_shutdown_server() | |
return '' | |
server.start() | |
server.join() | |
return q.get(block=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment