简体   繁体   English

os.setsid操作不被允许

[英]os.setsid operation not permitted

Alright bear with me there seems to be some answers on google about that but I simply cannot get it. 好吧忍受我在谷歌似乎有一些答案,但我根本无法得到它。

I'm running it after two forks in a Django Celery environment. 我在Django Celery环境中的两个分叉后运行它。 I don't know if these could have changed something but I assume they did not. 我不知道这些是否会改变一些东西,但我认为它们没有。

There is not much code to leave as a mighty 作为一个强大的人,没有太多的代码可以离开

if __name__ == '__main__':
    os.setsid()

grants me the beautiful Operation not permitted 授予我不允许的美丽行动

I'm running the latest stable Django, Debian, Celery and Python version. 我正在运行最新的稳定Django,Debian,Celery和Python版本。

Python's os.setsid() probably calls the underlying library call setsid(3) . Python的os.setsid()可能调用底层库调用setsid(3)

The full ERRORS section in man 3 setsid is: man 3 setsid的完整ERRORS部分是:

ERRORS
   EPERM  The  process group ID of any process equals the PID of the call-
          ing process.  Thus, in particular, setsid() fails if the calling
          process is already a process group leader.

IOW: the only cause for a setsid() failure, is when the calling process is already a process group leader. IOW: setsid()失败的唯一原因是调用进程已经是进程组负责人。 Ergo: you may ignore the failure. Ergo:你可以忽略失败。 To verify that this is the case, compare what you get back from getpid() and getpgid() when os.setsid() fails: 要验证是否是这种情况,请在os.setsid()失败时比较从getpid()getpgid() os.setsid()

#!/usr/bin/env python

import os
import errno

if __name__ == '__main__':
    try:
        os.setsid()
    except OSError, (err_no, err_message):
        print "os.setsid failed: errno=%d: %s" % (err_no, err_message)
        print "pid=%d  pgid=%d" % (os.getpid(), os.getpgid(0))

When I run the above I get: 当我运行以上内容时,我得到:

os.setsid failed: errno=1: Operation not permitted
pid=17025  pgid=17025

Note that the process-id (pid) is equal to the process-group-id (pgid), meaning that this process is indeed already a process group leader. 请注意,process-id(pid)等于process-group-id(pgid),这意味着此进程确实已经是进程组领导者。

PS: Yes, it is a baffling feature of python to raise exceptions where a simple error return code would suffice to distinguish success from failure (just like the familiar Un*x libc APIs behave). PS: 是的,python的一个令人困惑的功能是引发异常,其中一个简单的错误返回代码足以区分成功与失败(就像熟悉的Un * x libc API行为一样)。 This is unfortunately how the python system call interface is implemented, so you need to wrap many system calls with try: except ...: constructs to prevent python from aborting your code. 遗憾的是,如何实现python系统调用接口,因此需要使用try: except ...: construct来包装许多系统调用,以防止python中止代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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