flexx icon indicating copy to clipboard operation
flexx copied to clipboard

[Question] Using flexx to serve application in AWS (with Apache)

Open mateoespinosa opened this issue 3 years ago • 1 comments

Hey everyone! I am currently using flexx to build a dynamic frontend for a data-manipulation application whose backend is completely written in Python. I've been truly loving using this framework and it has allowed me to write significant portions of the frontend without much tweaking :)

I recently hit a hard wall when trying to deploy this application in AWS. For some context, I am trying to simply host this application in an AWS instance for me to be able to share the application with my research group using a public IP. So I don't expect a lot of traffic and I am more than ok with potential slowdowns. Nevertheless, I have not been able to set it up after trying multiple different ways of getting it to play nicely with Apache2 + WSGI. I also encountered similar issues when deploying it as a Google Cloud Platform service.

What happened in both cases is that the application opens up, shows the "Starting Flexx app" message, and then crashes with a "Lost connection with server" message before even showing any of the widgets. Note that this is an application that worked perfectly fine locally.

When looking at the Apache error logs in the AWS case, I see the following:

Exception ignored in: <function BaseEventLoop.__del__ at 0x7feaf07bb670>
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/base_events.py", line 654, in __del__
NameError: name 'ResourceWarning' is not defined
Exception ignored in: <function BaseEventLoop.__del__ at 0x7feaf07bb670>
Traceback (most recent call last):
  File "/usr/lib/python3.8/asyncio/base_events.py", line 654, in __del__
NameError: name 'ResourceWarning' is not defined
[Thu Feb 25 20:32:34.698294 2021] [mpm_event:notice] [pid 17535:tid 140647343914048] AH0049
1: caught SIGTERM, shutting down
[Thu Feb 25 20:32:34.788212 2021] [mpm_event:notice] [pid 17657:tid 139761221467200] AH0048
9: Apache/2.4.41 (Ubuntu) mod_wsgi/4.7.1 Python/3.8 configured -- resuming normal operation
s
[Thu Feb 25 20:32:34.788380 2021] [core:notice] [pid 17657:tid 139761221467200] AH00094: Co
mmand line: '/usr/sbin/apache2'
[Thu Feb 25 20:32:37.346286 2021] [wsgi:error] [pid 17659:tid 139761200736000] [I 20:32:37 
flexx.app] Asset store collected 2 new modules.
[Thu Feb 25 20:32:37.897703 2021] [wsgi:error] [pid 17659:tid 139761200736000] [I 20:32:37 
flexx.app] Serving apps at http://localhost:49190/
[Thu Feb 25 20:32:37.908456 2021] [wsgi:error] [pid 17659:tid 139761200736000] [client 131.
111.223.65:60480] [I 20:32:37 flexx.app] Asset store collected 25 new modules.
[Thu Feb 25 20:32:38.193025 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480] mod_wsgi (pid=17659): Exception occurred processing WSGI script '/var/www
/html/flexxapp/flexxapp.wsgi'., referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197242 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480] Traceback (most recent call last):, referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197289 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]   File "/usr/local/lib/python3.8/dist-packages/tornado/wsgi.py", line 222
, in __call__, referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197296 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]     self.application(request), referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197304 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]   File "/usr/local/lib/python3.8/dist-packages/tornado/web.py", line 2041
, in __call__, referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197308 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]     return dispatcher.execute(), referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197315 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]   File "/usr/local/lib/python3.8/dist-packages/tornado/web.py", line 2171
, in execute, referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197320 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]     self.handler._execute(transforms, *self.path_args,, referer: http://3
.9.118.88/
[Thu Feb 25 20:32:38.197327 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]   File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 288,
 in wrapper, referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197331 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]     future = _create_future(), referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197338 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]   File "/usr/local/lib/python3.8/dist-packages/tornado/gen.py", line 186,
 in _create_future, referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197342 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]     future = Future(), referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197349 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]   File "/usr/lib/python3.8/asyncio/events.py", line 639, in get_event_loo
p, referer: http://<MY_IP>/
[Thu Feb 25 20:32:38.197353 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480]     raise RuntimeError('There is no current event loop in thread %r.', re
ferer: http://<MY_IP>/
[Thu Feb 25 20:32:38.199051 2021] [wsgi:error] [pid 17659:tid 139761121490688] [client 131.
111.223.65:60480] RuntimeError: There is no current event loop in thread 'Dummy-1'., refere
r: http://<MY_IP>/

I was able to recreate this using a very simple flexx application so I don't expect it to be something specific to the application I wrote. I did this using the following application:

class Example(flx.Widget):

    def init(self):
        flx.Button(text='hello')
        flx.Button(text='world')
flx_app = flx.App(Example)
flx_app.serve('')
app = tornado.wsgi.WSGIAdapter(flx.current_server().app)

if __name__ == '__main__':
    flx.run()

Together with the following simple WSGI script:

#! /usr/bin/python3
#flexxapp.wsgi

import sys
sys.path.insert(0, '/var/www/html/flexxapp')

from flexxapp import app as application

If anyone has had any luck setting a flexx application in an AWS/GCP instance, do you have any insights on how to make it work? I am more than happy to share more code for fully recreating this if it makes things easier; just please let me know.

It would be amazing if I could simply use all the code I have already written for my application to serve it in a public domain. I imagine this is possible as there are several examples of flexx applications running in the documentation pages. Would it then be possible to maybe add some documentation on how would one set up such application?

mateoespinosa avatar Feb 25 '21 21:02 mateoespinosa

[...] Apache2 + WSGI

I think this is where it goes wrong. Flexx will want to setup a websocket, and when this fails, it considers this a lost connection. Are you running other services on this VM? If not, just skip the middleware and run the server from Python directly. If you want https, you could try putting an https endpoint in between.

almarklein avatar Mar 01 '21 10:03 almarklein