【问题标题】:running uwsgi in supervisor goes wrong but goes well in command line在主管中运行 uwsgi 出错但在命令行中运行良好
【发布时间】:2017-07-08 06:11:36
【问题描述】:

我在 supervisord 下运行 uwsgi 时遇到了一些问题。

主要问题是我用命令行运行uwsgi --ini /home/satori/mysite/src/mysite_uwsgi.ini,一切正常,但是当我在supervisorctl下运行它时,当我上传一个非ASCII字符名称的文件时,服务器返回500。

但它不会发生如果我在命令行中运行,文件已正确上传。

我认为问题与我的主管配置文件有关,可能是一些环境问题。我的配置文件在/etc/supervisord.conf

这是我的 mysite_uwsgi.ini 文件,位于/home/satori/mysite/src/mysite_uwsgi.ini

# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /home/satori/mysite/src
# Django's wsgi file
module          = mysite.wsgi
# the virtualenv (full path)
#home            = /home/ubuntu/mysite/bin

#plugins         = python34

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 10
# the socket (use the full path to be safe
socket          = /home/satori/mysite/src/mysite.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

这是 supervisord.conf 中的相关设置

[program:mysite]
command=uwsgi --ini /home/satori/mysite/src/mysite_uwsgi.ini
autostart=true
autorestart=true
user=root
log_stderr=true
stdout_logfile=/var/log/mysite.log

这是 500 错误:

UnicodeEncodeError at /upload/view/
'ascii' codec can't encode characters in position 37-38: ordinal not in range(128)

models.py

# encoding: utf-8
import os
from django.db import models
from django.conf import settings


def get_upload_path(instance, filename):
    filename = ''.join([ch for ch in filename if ord(ch) < 128])
    return os.path.join("user_%d" % instance.owner.id, filename)


class Picture(models.Model):
    """This is a small demo using just two fields. The slug field is really not
    necessary, but makes the code simpler. ImageField depends on PIL or
    pillow (where Pillow is easily installable in a virtualenv. If you have
    problems installing pillow, use a more generic FileField instead.
    """
    file = models.ImageField(upload_to=get_upload_path)
    slug = models.SlugField(max_length=50, blank=True)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

    def __str__(self):
        return self.file.name

    @models.permalink
    def get_absolute_url(self):
        return ('upload-new', )

    def save(self, *args, **kwargs):
        self.slug = self.file.name
        super(Picture, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
        """delete -- Remove to leave file."""
        self.file.delete(False)
        super(Picture, self).delete(*args, **kwargs)

【问题讨论】:

    标签: uwsgi supervisord


    【解决方案1】:

    你应该把它添加到你的 uwsgi.ini 文件中

    env = PYTHONIOENCODING=UTF-8

    另外,不确定这是否是您所做的,但我使用 str 而不是 unicode。我正在使用一个需要 str 的插件,在这种情况下你可以写:

    def __str__(self):
        return YourModel.__unicode__(self)
    

    【讨论】:

    • 感谢您的回复,但它不起作用。我转而删除文件名中的非 ascii 字符,它工作正常。据我所知,编码一直是个大问题。
    • 我将我的模型添加到问题中,如果您有任何想法,请告诉我。非常感谢。
    • 在您的模型中似乎没有 unicode 方法。添加那个并返回 self.file.name。然后添加一个 str 方法以备不时之需,就像我编辑的帖子一样。这对我有用。它不应该与您的 uwsgi 和 supervisord 有关。虽然我认为在开发过程中最好将进程设置为 1。
    • 谢谢兄弟,但它不起作用。我坚持 supervisorctl 设置,你知道,在命令行中运行良好意味着它与代码无关。所以我参考了更多的问题。现在我找到了答案。答案是将environment=LANG=en_US.UTF-8, LC_ALL=en_US.UTF-8, LC_LANG=en_US.UTF-8 添加到主管设置文件的[supervisord] 部分。这是我找到解决方案的地方。link。无论如何,非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 2012-01-18
    • 2017-04-16
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多