简体   繁体   中英

ImportError from Flask App with Uwsgi: handle multiple parallel requests with Flask

I would like to serve a simple Flask App with uWSGI, in order to have a Simple uWSGI deployment with 4 processes (and hence respond to multiple parallel requests).

I created the "simple_app.py" file containing the application:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello World!"

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

And a "uwsgi.ini" configuration file:

[uwsgi]
socket = 0.0.0.0:5000
protocol = http
module = simple_app:app
threads = 1
processes = 4

However, when I try to start the server on my local Macbook machine with the Terminal command uwsgi uwsgi.ini , it raises the follwing import error:

*** Operational MODE: preforking ***
Traceback (most recent call last):
  File "./simple_app.py", line 9, in <module>
    from flask import Flask
  File "/anaconda3/lib/python3.7/site-packages/flask/__init__.py", line 14, in <module>
    from jinja2 import escape
  File "/anaconda3/lib/python3.7/site-packages/jinja2/__init__.py", line 33, in <module>
    from jinja2.environment import Environment, Template
  File "/anaconda3/lib/python3.7/site-packages/jinja2/environment.py", line 15, in <module>
    from jinja2 import nodes
  File "/anaconda3/lib/python3.7/site-packages/jinja2/nodes.py", line 19, in <module>
    from jinja2.utils import Markup
  File "/anaconda3/lib/python3.7/site-packages/jinja2/utils.py", line 16, in <module>
    from jinja2._compat import text_type, string_types, implements_iterator, \
  File "/anaconda3/lib/python3.7/site-packages/jinja2/_compat.py", line 31, in <module>
    import pickle
  File "/anaconda3/lib/python3.7/pickle.py", line 33, in <module>
    from struct import pack, unpack
  File "/anaconda3/lib/python3.7/struct.py", line 13, in <module>
    from _struct import *
ImportError: dlopen(/anaconda3/lib/python3.7/lib-dynload/_struct.cpython-37m-darwin.so, 2): Symbol not found: _PyByteArray_Type
  Referenced from: /anaconda3/lib/python3.7/lib-dynload/_struct.cpython-37m-darwin.so
  Expected in: flat namespace
 in /anaconda3/lib/python3.7/lib-dynload/_struct.cpython-37m-darwin.so
unable to load app 0 (mountpoint='') (callable not found or import error)


   *** no app loaded. going in full dynamic mode ***
   *** uWSGI is running in multiple interpreter mode ***
    spawned uWSGI worker 1 (pid: 53858, cores: 1)
    spawned uWSGI worker 2 (pid: 53859, cores: 1)
    spawned uWSGI worker 3 (pid: 53860, cores: 1)
    spawned uWSGI worker 4 (pid: 53861, cores: 1)

Both Files are in the same folder of my system; how can I correctly launch the app and receive multiple parallel requests?

I've tried to recreate your situation and it's working as expected. Here's my uwsgi.ini file:

[uwsgi]
socket = 192.168.1.219:8888
protocol = http
module = simple_app:app
threads = 1
processes = 4

And here's the simple_app.py file:

from flask import Flask
app = Flask(__name__)


@app.route('/')
def hello():
    return "Hello World!"


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

And I'm starting up my uwsgi server by uwsgi uwsgi.ini command.

I think the issue is not in uwsgi or your application code, but with the Python or Flask/Jinja installation. Make sure you have properly installed Flask and Jinja in your anaconda environment. You can try to install Python from scratch on your host, or try using Docker and any other virtual environment to isolate Python installation and modules installed on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM