Problem with Nose testing until forking process

Discussion in 'Python' started by Jan Koprowski, Jan 26, 2009.

  1. Hi !

    I write application witch sometimes need fork to shell based process
    (some kind of shell command). I snatch stdin, stdout, stderr and two
    additional streams and fork process to run command and get results.

    # -*- encoding: utf-8 -*-

    import os
    import sys
    import subprocess

    def pipes_function():

    global login_r, login_w, password_r, password_w

    try: os.fdopen(3).close()
    except OSError: pass
    try: os.fdopen(4).close()
    except OSError: pass

    os.close(login_w)
    os.close(password_w)
    os.dup2(login_r, 3)
    os.dup2(password_r, 4)

    class BrokerProcess:

    __slots__ = ['username', 'password', 'command', 'stdout',
    'stderr', 'returned']

    def run(self):

    global login_r, login_w, password_r, password_w

    login_r, login_w = os.pipe()
    password_r, password_w = os.pipe()

    wrapper = subprocess.Popen(args=('/usr/bin/special/wrapper'),
    preexec_fn=pipes_function, bufsize=0, stdin=subprocess.PIPE,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    os.close(login_r)
    os.close(password_r)

    login_w = os.fdopen(login_w, 'w')
    password_w = os.fdopen(password_w, 'w')

    login_w.write(self.username)
    login_w.close()

    password_w.write(self.password)
    password_w.close()

    self.command = self.command.encode()
    wrapper.stdin.write(self.command)
    wrapper.stdin.close()
    self.stdout = wrapper.stdout.read()
    self.stderr = wrapper.stderr.read()

    self.returned = wrapper.wait()
    return self.returned


    My problem is that when I run nose test for function witch use this
    broker i get followin errors (I test Pylons app):

    File "/home/users/matrix/johny/Pylons/zhradmin/zhradmin2/zhradmin2/
    controllers/account.py",
    line 51, in login
    account.login()
    File "/home/users/matrix/johny/Pylons/zhradmin2/zhradmin2/model/
    account.py",
    line 29, in login
    File "/home/users/matrix/johny/Pylons/zhradmin2/zhradmin2/lib/
    brokerprocess.py",
    line 41, in run
    File "/usr/local/lib/python2.5/subprocess.py", line 594, in __init__
    errread, errwrite)
    File "/usr/local/lib/python2.5/subprocess.py", line 1091, in
    _execute_child
    raise child_exception
    OSError: [Errno 9] Bad file descriptor

    Is anyone know why I get error ?
    Jan Koprowski, Jan 26, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Smith

    Forking sub-process in CppUnit?

    Roy Smith, Jul 4, 2003, in forum: C++
    Replies:
    0
    Views:
    383
    Roy Smith
    Jul 4, 2003
  2. Andrew Robert

    Process forking on Windows

    Andrew Robert, May 17, 2006, in forum: Python
    Replies:
    9
    Views:
    2,183
    Benji York
    May 19, 2006
  3. forking a process

    , Sep 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    433
    SM Ryan
    Sep 18, 2005
  4. John Connor
    Replies:
    0
    Views:
    237
    John Connor
    Apr 8, 2011
  5. Roald
    Replies:
    1
    Views:
    116
    R. Osinga
    Feb 2, 2013
Loading...

Share This Page