I would suggest using a simple python micro web framework like a bottle. Then you send requests to the background process through the queue (so that the connection ends).
Then the background process will have a continuous cycle that will check your conditions (time and number) and perform the task after the condition is met.
Edit:
Here is an example of a web server that sells items before sending them to any queue system that you want to use (RabbitMQ always seemed to me too complicated with Python. I used Celery and other simpler queue systems before). Thus, the backend just grabs one "element" from the queue, which will contain all the required 50 requests.
import bottle import threading import Queue app = bottle.Bottle() app.queue = Queue.Queue() def send_to_rabbitMQ(items): """Custom code to send to rabbitMQ system""" print("50 items gathered, sending to rabbitMQ") def batcher(queue): """Background thread that gathers incoming requests""" while True: batcher_loop(queue) def batcher_loop(queue): """Loop that will run until it gathers 50 items, then will call then function 'send_to_rabbitMQ'""" count = 0 items = [] while count < 50: try: next_item = queue.get(timeout=.5) except Queue.Empty: pass else: items.append(next_item) count += 1 send_to_rabbitMQ(items) @app.route("/add_request", method=["PUT", "POST"]) def add_request(): """Simple bottle request that grabs JSON and puts it in the queue""" request = bottle.request.json['request'] app.queue.put(request) if __name__ == '__main__': t = threading.Thread(target=batcher, args=(app.queue, )) t.daemon = True
The code used to verify it:
import requests import json for i in range(101): req = requests.post("http://localhost:8080/add_request", data=json.dumps({"request": 1}), headers={"Content-type": "application/json"})
source share