import mock_service_app
import multiprocessing as mp
proc = mp.Process(target=mock_service_app.start_app, args=())
proc.start()
proc = mp.Process(target=start_app, args=(), initializer=mute)
configure_error_handlers
is somehow a standard block which is in all my Flask / Quart projects, but without jsonify it's even more beautiful. Does it respect the JSON encoder I set here without jsonify: https://github.com/openbikebox/connect/blob/master/webapp/common/filter.py#L13 ?
hey everyone!
hope you are all well
I am trying to learn how to make an ML-based web app using Quart.
I want to build an application that can handle prediction of a batch of images. I want to divide the batch into small chunks, make predictions and return the response to the user. This process should be done until the last chunk
I thought of making an async recursive api call. All I want to do is to make it recursively return the response.
Any help is appreciated.
This is the function that I want to call it recursively.
@app.route('/predict', methods=['POST'])
async def predict():
if request.method == 'POST':
has_files = (await request.files).get("file", None)
if has_files:
files = (await request.files).getlist("file")
responses= {}
# split files into small chunks to place here !
for file, i in zip(files, range(len(files))):
img_bytes = file.read()
class_id, class_name = await get_prediction(model=model,
image_bytes=img_bytes,
imagenet_class_index= imagenet_class_index)
responses[i] = {'class_id': class_id, 'class_name': class_name}
print(responses[i])
return await render_template("results.html", text=str(responses))
else:
return await render_template("results.html", text="please upload image!")
@pgjones when using websocket.close()
from latest quart (from master
branch), i get an error because quart is trying to close the websocket again in handle_websocket
I made a hotfix for hypercorn that forces only one websocket.close
event to be executed, ignoring the rest. I did this in hypercorn because quart is not aware of the websocket state, and so it can not know if the websocket has closed (or is in the process of doing so), does this make sense? Is it correct to MR this fix?
Another question: Is hypercorn going to support HTTP&Websockets ASGI 2.3? reason
is not a blocker on my webapp development, but it is wanted in the future (this would also mean support for 2.2, since hypercorn is currently on 2.1)
hypercorn[497166]: [2021-05-03 18:07:08 +0000] [497166] [ERROR] Error in ASGI Framework
hypercorn[497166]: Traceback (most recent call last):
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/hypercorn/trio/context.py", line 39, in _handle
hypercorn[497166]: await invoke_asgi(app, scope, receive, send)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/hypercorn/utils.py", line 239, in invoke_asgi
hypercorn[497166]: await app(scope, receive, send)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart/app.py", line 2117, in __call__
hypercorn[497166]: await self.asgi_app(scope, receive, send)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart/app.py", line 2140, in asgi_app
hypercorn[497166]: await asgi_handler(receive, send)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart_trio/asgi.py", line 18, in __call__
hypercorn[497166]: nursery.start_soon(self.handle_request, nursery, request, send)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/trio/_core/_run.py", line 741, in __aexit__
hypercorn[497166]: raise combined_error_from_nursery
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart_trio/asgi.py", line 29, in handle_request
hypercorn[497166]: response = await self.app.handle_request(request)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart_trio/app.py", line 135, in handle_request
hypercorn[497166]: return await self.handle_exception(error)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart/app.py", line 1118, in handle_exception
hypercorn[497166]: raise error
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart_trio/app.py", line 131, in handle_request
hypercorn[497166]: return await self.full_dispatch_request(request_context)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart_trio/app.py", line 153, in full_dispatch_request
hypercorn[497166]: result = await self.handle_user_exception(error)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart_trio/app.py", line 166, in handle_user_exception
hypercorn[497166]: return await super().handle_user_exception(error)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart/app.py", line 1104, in handle_user_exception
hypercorn[497166]: raise error
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart_trio/app.py", line 151, in full_dispatch_request
hypercorn[497166]: result = await self.dispatch_request(request_context)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/quart/app.py", line 1933, in dispatch_request
hypercorn[497166]: return await handler(**request_.view_args)
hypercorn[497166]: File "/root/lnbits/lnbits/decorators.py", line 28, in wrapped_view
hypercorn[497166]: return await view(**kwargs)
hypercorn[497166]: File "/root/lnbits/lnbits/core/views/api.py", line 313, in api_payments_sse
hypercorn[497166]: g.nursery.start_soon(payment_received)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/trio/_core/_run.py", line 912, in start_soon
hypercorn[497166]: GLOBAL_RUN_CONTEXT.runner.spawn_impl(async_fn, args, self, name)
hypercorn[497166]: File "/root/lnbits/venv/lib/python3.8/site-packages/trio/_core/_run.py", line 1357, in spawn_impl
hypercorn[497166]: raise RuntimeError("Nursery is closed to new arrivals")
hypercorn[497166]: RuntimeError: Nursery is closed to new arrivals
g
. and once it appeared first then for the next 24 hours it kept repeating so the SSE endpoint didn't work (although the rest of the app seemed to work)
KeyError
when accessing a dict item, for example) would cause the app to stop responding, but not crash, and no more error would show up in the logs ever. the only thing that would show up in the logs are these exact same nursery errors, always at this exact SSE handler.
RuntimeError
and exiting the app when it happened so it could be restarted, but that sounded like a terrible hack, that's why I'm asking here what is the proper way to do it
app
?
app.nursery
already exists by default
app
is not exposed anywhere because someone thought that was best practices so I can't access it from my handlers
app
?