I have a Flask API with an endpoint that has a problem: one user should send only one request for some period of time. In pseudocode it looks like the following:
session_id = user.get('current_session_id')
current_session = get_current_session(session_id)
secret_variable = current_session.get('secret_variable')
if secret_variable:
return {'error': 'user already has secret variable'}
secret_variable = perform_secret_request() # this one is time consuming, so user shouldn't run many of them
current_session.add_values({'secret_variable': secret_variable}) # later variable will be removed from the session, but it doesn't matter here
I've tried uwsgi.lock() for this purpose, but it works only for one process with one thread. When I increase the number of threads or/and processes it stops working. Example within pseudocode:
session_id = user.get('current_session_id')
current_session = get_current_session(session_id)
uwsgi.lock()
secret_variable = current_session.get('secret_variable')
uwsgi.unlock()
if secret_variable:
return {'error': 'user already has secret variable'}
secret_variable = perform_secret_request() # this one is time consuming, so user shouldn't run many of them
current_session.add_values({'secret_variable': secret_variable}) # later variable will be removed from the session, but it doesn't matter here
How I can lock this problem part? Are there any alternative solutions?