unbit / uwsgi

uWSGI application server container
http://projects.unbit.it/uwsgi
Other
3.45k stars 686 forks source link

Optimize throughput while accessing limited resources for multiple parallel requests, code inside #2381

Open dexception opened 2 years ago

dexception commented 2 years ago

I have a situation where I have some limited objects lets say from 0 to 5. Now I want users to access them with maximum efficiency. In order to access the limited objects I need an index for which I have to use locks to find which is available. I am currently getting throughput of 13(tested via jmeter) I am not able to scale as this is killing my performance.

Is there any other way to resolve this problem ?

image

from flask import Flask
import threading
import time
from time import sleep
import uwsgi

app = Flask(__name__)

uwsgi.cache_set("0", "free")
uwsgi.cache_set("1", "free")
uwsgi.cache_set("2", "free")
uwsgi.cache_set("3", "free")
uwsgi.cache_set("4", "free")
uwsgi.cache_set("5", "free")

def getResource():
    idx = -1
    while True:
        if uwsgi.cache_exists("0"):
            idx = 0
            break
        elif uwsgi.cache_exists("1"):
            idx = 1
            break
        elif uwsgi.cache_exists("2"):
            idx = 2
            break
        elif uwsgi.cache_exists("3"):
            idx = 3
            break
        elif uwsgi.cache_exists("4"):
            idx = 4
            break
        elif uwsgi.cache_exists("5"):
            idx = 5
            break

    uwsgi.cache_del(str(idx))
    return idx 

def releaseResource(idx):
    uwsgi.cache_set(str(idx), "free")

@app.route("/", methods=['GET'])
def hello_world():
    uwsgi.lock()
    idx = getResource()
    uwsgi.unlock()
    #print("GPU idx::"+str(idx))
    sleep(0.3)
    releaseResource(idx)
    return "<p>Hello, World!</p>"
dexception commented 2 years ago

Can any expert share some knowledge? I am really stuck.

dexception commented 2 years ago

Any updates ?

unbit commented 2 years ago

@dexception which platform/locking system are you using ? in my experience on some system using sysv ipc gives better results (--lock-engine ipcsem).

dexception commented 2 years ago

ubuntu