python - Batching and queueing in a real-time webserver -


added server designi need webserver routes incoming requests back-end workers batching them every 0.5 second or when has 50 http requests whichever happens earlier. way implement in python/tornado or other language?

what thinking publish incoming requests rabbitmq queue , somehow batch them before sending back-end servers. can't figure out how pick multiple requests rabbitmq queue. point me right direction or suggest alternate apporach?

i suggest using simple python micro web framework such bottle. send requests background process via queue (thus allowing connection end).

the background process have continuous loop check conditions (time , number), , job once condition met.

edit:

here example webserver batches items before sending them queuing system want use (rabbitmq seemed overcomplicated me python. have used celery , other simpler queuing systems before). way backend grabs single 'item' queue, contain required 50 requests.

import bottle import threading import queue   app = bottle.bottle()  app.queue = queue.queue()   def send_to_rabbitmq(items):     """custom code send rabbitmq system"""     print("50 items gathered, sending rabbitmq")   def batcher(queue):     """background thread gathers incoming requests"""     while true:         batcher_loop(queue)   def batcher_loop(queue):     """loop run until gathers 50 items,     call 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 grabs json , puts in queue"""     request = bottle.request.json['request']     app.queue.put(request)   if __name__ == '__main__':     t = threading.thread(target=batcher, args=(app.queue, ))     t.daemon = true  # make sure background thread quits when program ends     t.start()      bottle.run(app) 

code used test it:

import requests import json  in range(101):     req = requests.post("http://localhost:8080/add_request",                         data=json.dumps({"request": 1}),                         headers={"content-type": "application/json"}) 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -