This is a more general question on how the solution could be designed.
The goal is to have an API which would take some a text or a list of texts, process it and return transformed ones. How should I approach it so that whenever a text is requested, all the requests that contains list of texts would wait and one text would be processed first?
2
Answers
In general you would have a heap (priority queue) of requests in line. Your API can fetch highest-priority requests from this heap.
In this particular case, if all you need is
str
s first andlist
s second, define your priority function likelambda x: 2 if isinstance(x, str) else 1
.Note that this does not take into account of potential timeouts.. if you have lots of
str
requests, thelist
ones could have to wait a while.This question is a bit generic, therefore I’m going to answer with a stack that I developed in a past project. I would use the redis-server function of streaming(https://redis.io/topics/streams-intro). You can have a stream for a list of texts and a stream for texts, and when your algorithm is searching for new tasks he first checks if the stream of the list of texts has any pending package. I recommend the usage of redis-server because is a robust solution, having access to a server dedicated for this service, instead of using celery. But I have never used celery for this purpose, and can’t advocate for him on this topic.