bottlepy / bottle

bottle.py is a fast and simple micro-framework for python web-applications.
http://bottlepy.org/
MIT License
8.37k stars 1.46k forks source link

Bottle block when get many post request #1214

Closed mucthienton closed 4 years ago

mucthienton commented 4 years ago

I'm use Bottle 0.12.17 and python3, my server get file bytes from body and use opencv resize and save it.

import cv2
import numpy as np
from bottle import Bottle, run, request,response, static_file, abort

bottle_app = Bottle()
@bottle_app.route('/img/resize',method=['POST'])
def resize():
    testkey = request.get_header('testkey')
    if testkey != '123zbc1234':
        abort(code=400)
        return        
    buff = request.body.read()
    np_buff = np.frombuffer(buff,dtype='uint8')
    img = cv2.imdecode(np_buff,cv2.IMREAD_COLOR)
    #calculate the 50 percent of original dimensions
    scale_percent = 50
    width = int(img.shape[1] * scale_percent / 100)
    height = int(img.shape[0] * scale_percent / 100)
    dsize = (width, height)
    output = cv2.resize(src, dsize)
    cv2.imwrite('cv2-resize-image-50.png',output)
    # this code was added after problem happen, but it's useless
    request.body.close()
    return

Server run normally but client request and server didn't response When I press Ctrl + C server show:

File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 979, in call
return self.wsgi(environ, start_response)
File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 954, in wsgi
out = self._cast(self._handle(environ))
File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 862, in _handle
return route.call(*args)
File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 1742, in wrapper
rv = callback(
a, **ka)
File "server.py", line 350, in checkFake
buff = request.body.read()
File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 1197, in body
self._body.seek(0)
File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 166, in get
if key not in storage: storage[key] = self.getter(obj)
File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 1166, in _body for part in body_iter(read_func, self.MEMFILE_MAX): File "/home/wee1/.local/lib/python3.6/site-packages/bottle.py", line 1129, in _iter_body part = read(min(maxread, bufsize)) File "/usr/lib/python3.6/socket.py", line 586, in readinto return self._sock.recv_into(b)

After, client request and get response normal. This problem happen nearly random and I press Ctrl + C to fix it. Can anyone help, thanks.

oz123 commented 4 years ago

You are using wsgiref the default server which isn't able to handle more than a few requests per second. You should really use gunicorn or uwsgi.

mucthienton commented 4 years ago

Thanks @oz123, I'm trying gunicorn in two days and no problem.

oz123 commented 4 years ago

You should close the issue then