I have a python script(server) called main.py
which accepts client requests and add data to a Queue. There are few python worker threads which are checking on that queue. If worker is free and queue is not empty, worker takes an element from the queue, process and send response back to client. Clients should wait until a worker perform the task. How can I do this in python? Basically how can I identify to which client I should send back data. What we normally do is return the value from the serve function at main.py
using flask.jsonify()
. But I cannot do it here because the processing may probably happen at a later time when threads are free.
Worker threads are in another python script. say worker.py
Here is my main.py
from queue import Queue
import flask
import threading
# initialize flask application
app = flask.Flask(__name__)
## Creating pools
workers = Queue(10)
tasks = Queue(10)
taskLock = threading.Lock()
@app.route('/predict', methods=["POST"])
def serve():
if flask.request.method == "POST":
if flask.request.files.get('image'):
tasks.put(flask.request.files["image"].read())
??
return flask.jsonify(("wait..."))
if __name__ == "__main__":
print("Server is running...")
app.run(host='0.0.0.0')
worker.py code
class predictThread(threading.Thread):
def __init__(self, threadID, name, que, lock):
threading.Thread.__init__(self)
self.threadID =threadID
self.name = name
self.que = que
self.lock = lock
def run(self):
print("starting " + self.name + " thread")
work(self.que, self.lock)
print("Exiting " + self.name + " thread")
def work(que, lock):
while True:
if que.empty():
time.sleep(2)
else:
lock.acquire()
data = que.get()
lock.release()
# process data
Assume worker threads are running in workers queue in main.py
. I haven't created them yet.