'IOError:[Errno 9]在守护进程中调用子进程时文件描述符错误

时间:2017-12-14 21:05:34

标签: python python-2.7

                <?php         

                $loop = new WP_Query(array(
                                'posts_per_page' => 6,
                                'post_status' => 'publish',
                                'post_type' => 'post',
                                'orderby'  => array('meta_value_num' => 'DESC', 'date' => 'DESC'),
                                'meta_key' => '_liked',
                                    'date_query' => array(
                                     array(
                                    'after' => 'monday previous week',
                                    'before' => 'monday next week'
                                       ),
                                     ),
                                'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
                                     ));
                  while ($loop -> have_posts()) : $loop -> the_post();
            ?>

                <?php
                    /* Include the Post-Format-specific template for the content.
                     * If you want to overload this in a child theme then include a file
                     * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                     */


                    get_template_part( 'content', get_post_format() );              
                ?>

我得到IOError:[Errno 9]只有当main被称为deamon即(分叉进程并且stdin / stdout / stderr关闭

我怀疑它是关闭那些管道,但如果我没有,我的程序不会返回。有没有办法在守护进程中执行子进程?

1 个答案:

答案 0 :(得分:0)

我更改了代码:

if args.daemon:
    pid = os.fork()
    if pid > 0:
        sys.exit(0)
    os.close(0)
    os.close(1)
    os.close(2)

为:

 if args.daemon:
    stdin = '/dev/null'
    stdout = '/dev/null'
    stderr = '/dev/null'

    try:
        pid = os.fork()
        if pid > 0:
            # exit first parent
            sys.exit(0)
    except OSError, e:
        sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
        sys.exit(1)

    # decouple from parent environment
    os.chdir("/")
    os.setsid()
    os.umask(0)

    # do second fork
    try:
        pid = os.fork()
        if pid > 0:
            # exit from second parent
            sys.exit(0)
    except OSError, e:
        sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
        sys.exit(1)

    # redirect standard file descriptors
    sys.stdout.flush()
    sys.stderr.flush()
    si = file(stdin, 'r')
    so = file(stdout, 'a+')
    se = file(stderr, 'a+', 0)
    os.dup2(si.fileno(), sys.stdin.fileno())
    os.dup2(so.fileno(), sys.stdout.fileno())
    os.dup2(se.fileno(), sys.stderr.fileno())

和子进程不再引发IOError