获取Popen的模块句柄

时间:2013-11-18 22:40:19

标签: python python-2.7 popen

我正在尝试从打开的Popen对象中获取指定的对象,然后将其分配给模块中的全局内容,以便我可以从主函数中查询它。有点像这样:

class TestController (object):
    def __init__ (self):
        self.test_status=object
        print "You have initialized the TestController"
    def start_test(self, input):
        test_handle = Popen([args])
        self.test_status=test_handle
    def check_testStatus(self):
        print self.test_status.poll()


def main():
   print "Main function is now running"
   testHandle = TestController()
   testHandle.start_test("this")
   testHandle.check_testStatus()


def showmenu():
    prompt = """
(N)ew User Login
(E)xisting User Login
(Q)uit

Enter choice: """
    main()

    done = 0
    while not done:
        chosen = 0
        while not chosen:
            try:
                choice = raw_input(prompt)[0]
            except (EOFError, KeyboardInterrupt):
                choice = 'q'
            print '\nYou picked: [%s]' % choice

            if choice not in 'neqs':
                print 'invalid menu option, try again'
            else:
                chosen = 1

        if choice == 'q': done = 1
        if choice == 'n': newuser()
        if choice == 'e': olduser()
        if choice == 's': report_test_status()

if __name__ == '__main__':
    showmenu()

虽然不起作用。有人可以告诉我我做错了什么。

4 个答案:

答案 0 :(得分:0)

如果变量test_handle在模块名称空间中,则需要将其指定为global:

test_handle = None

class TestController (object):
    def __init__ (self):
        self.test_status=object
        print "You have initialized the TestController"
    def start_test(self, input):
        global test_handle
        test_handle = Popen([args])
        self.test_status=test_handle
    def check_testStatus(self):
        print self.test_status.poll()

答案 1 :(得分:0)

这是未经测试的,因为你没有给我们太多的继续。我选择使用类变量而不是全局变量,但您也可以使用全局变量。

class TestController (object):
    def __init__ (self):
        self.test_status=object
        print "You have initialized the TestController"
    def start_test(self, input):
        test_handle = Popen([args])
        TestController.test_status=test_handle
    def check_testStatus(self):
        print self.test_status.poll()

def main():
   print(TestController.test_status)

答案 2 :(得分:0)

使用一些存根函数,以下内容对我有用。对不起,我正在使用更新版本的Python。

from subprocess import Popen
test_handle = None    
class TestController (object):
    def __init__ (self):
        self.test_status=object
        print("You have initialized the TestController")
    def start_test(self, app):
        global test_handle
        test_handle = Popen(app)
        self.test_status=test_handle
    def check_testStatus(self):
        print(self.test_status.poll())

def report_test_status():
    "Report test status"

def main():
   print("Main function is now running")
   testHandle = TestController()
   testHandle.start_test("c:\\windows\\system32\\cmd.exe")
   testHandle.check_testStatus()


def showmenu():
    prompt = """
(N)ew User Login
(E)xisting User Login
(Q)uit

Enter choice: """
    main()

    done = 0
    while not done:
        chosen = 0
        while not chosen:
            try:
                choice = input(prompt)[0]
            except (EOFError, KeyboardInterrupt):
                choice = 'q'
            print('\nYou picked: [%s]' % choice)

            if choice not in 'neqs':
                print('invalid menu option, try again')
            else:
                chosen = 1

        if choice == 'q': done = 1
        if choice == 'n': newuser()
        if choice == 'e': olduser()
        if choice == 's': report_test_status()

if __name__ == '__main__':
    showmenu()

答案 3 :(得分:0)

我假设newuser()olduser()report_test_status()想要引用testHandle中定义的main()。我无法确定,因为您没有向我们展示这些功能的作用,并且您希望采取不同行为的代码部分并不明显。

所以,我认为你想要做的是:

靠近顶部,有:

testHandle = None

实际上并不是必需的,但允许阅读代码的人轻松看到有一个全局变量。

然后,使用main()关键字在global函数中将testHandle声明为全局:

def main():
    global testHandle
    print "Main function is now running"
    testHandle = TestController()
    testHandle.start_test("this")
    testHandle.check_testStatus()

这样,当您在main中分配给testHandle时,您将分配给全局(读取:模块级别)变量,而不是创建仅在函数内可见的新局部变量。

我想说这不是设计代码的好方法。我认为一个更好的解决方案是让你的main()函数返回testHandle对象,然后改为newuser()类和TestController类的其他方法。