Home  >  Article  >  Backend Development  >  Detailed explanation of uWSGI encoding problem solving method

Detailed explanation of uWSGI encoding problem solving method

高洛峰
高洛峰Original
2017-03-26 10:09:152413browse

Recently, an error occurred when an application written in Flask was deployed to the official server through Supervisor+uWSGI. I finally solved it by searching for relevant information, so I thought of sharing it with everyone. The following article mainly introduces how to solve the problem of uWSGI. For relevant information on coding issues, friends in need can refer to it.

Found the problem

I recently encountered a problem at work when deploying an application written in Flask to the official server through Supervisor+uWSGI , an error like this occurs:

Unable to print the message and arguments – possible formatting error.

or

UnicodeEncodeError: ‘ascii' codec can't encode characters in position 24-25: ordinal not in range(128)

Interestingly, there is no such error when running directly in the Python environment. It is also normal to use uwsgi uwsgi.ini to run this way.

Due to insufficient support for unicode, this error often appears in Python2, but all my programs are written in Python3, and such errors should not occur again. Moreover, all python files have the encoding set on the first line:

# -*- coding: utf-8 -*-

My environment is as follows:

  • Ubuntu 16.04.1 LTS

  • Python 3.5.2

  • ##uWSGI 2.0.14 (in python3 pip)

  • Supervisor 3.3.1 (in python2 pip)

uwsgi.ini configuration file content is as follows:

[uwsgi]
master = true

wsgi-file = manage.py
callable = app

processes = 2
threads = 2
max-requests = 6000
chmod-socket = 664

uid = app
gid = app

buffer-size = 32768

venv = {project_dir}/venv

; http = 127.0.0.1:5001

logto = {project_dir}/logs/uwsgi.log
Because neither Python nor uwsgi can be used directly Such an error occurs, so it can be judged that the problem should be caused by the environment encoding settings.

Check the server encoding as follows:

% locale
LANG=C
LANGUAGE=C:
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

It is found that the LANG and LANGUAGE environment variables are not set.

The values ​​of these two environment variables can be set in uwsgi.ini. After testing, I found out that what actually works is LANGUAGE .

env LANG="en_US.UTF-8"
env LANGUAGE="en_US.UTF-8"

The above is the detailed content of Detailed explanation of uWSGI encoding problem solving method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn