How to do parallel processing while using gevent with WSGIServer?

Viewed 12

I am running WSGIServer using gevent but I also want to run a background worker.

from gevent import monkey
from gevent.pywsgi import WSGIServer
from flask import Flask, request,Response

monkey.patch_all()
from threading import Thread
api = Flask(__name__)

my_queue=[1,2,3]

@api.route('/add', methods=['GET'])
def add():
    my_queue.append(request.args.get('item'))
    return Response("Added!", 200)

def worker(id):
    while True:
        print(f"{id}-working-{my_queue}")
        
if __name__ == '__main__':
    server = WSGIServer(('0.0.0.0', 5001), api)
    server_thread = Thread(target=server.start)
    
    worker1_thread = Thread(target=worker,args=(1,))
    worker2_thread = Thread(target=worker,args=(2,))

    server_thread.start()
    worker1_thread.start()
    worker2_thread.start()

If I am running it without monkey.patch_all(), my two worker works in parallel but the API will not respond. If I use monkey.patch_all() only the first worker will work and the API will also not respond.

How can I get this to work properly?

Also, I am aware I need a lock for the queue but I don't know how to implement it.

0 Answers
Related