子进程在监督下运行时失败

2024-05-29 08:29:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用于发送邮件的supervisor(e)d python脚本。当我ssh并从shell运行脚本时,一切正常。输出是相同的,当然没有失败,evalue是0

evalue = -1
try:
   f = tempfile.TemporaryFile()
   f.write(body.encode('utf-8'))
   f.seek(0)
   log.debug("'%s'" % "' '".join(call))
   for s in call:
       log.debug(type(s))
   evalue = subprocess.check_call(call, stdin=f)
except Exception as e:
    log.exception(e)
finally:
    f and f.close()
    f = None
log.debug("evalue %s" % evalue)

我看到了输出:

^{pr2}$

shell中的区域设置是es_埃斯UTF-在监督官会议上

environment=LC_ALL=es_ES.UTF-8

谢谢。在


Tags: debug脚本loges邮件shellcalltempfile
1条回答
网友
1楼 · 发布于 2024-05-29 08:29:53

我可以通过在C语言环境中运行脚本来重现您的问题:

$ LANG=C python run-subprocess.py
# -> TypeError: execv() arg 2 must contain only strings

在这种情况下,所有编码都是ascii码:

^{pr2}$

如果您强制utf-8编码,则它可以工作:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from subprocess import check_call

cmd = [b'-s', u'test ñññ ', b'-a', u'/tmp/test_ñññ_0,00_E.pdf', b'-r', u'test']
encoding = 'utf-8' # force utf-8 no matter what
                   # `sys.getfilesystemencoding()` or
                   # `locale.getpreferredencoding(True)` say
check_call(['echo'] + [s.encode(encoding) if isinstance(s, unicode) else s
                       for s in cmd])

相关问题 更多 >

    热门问题