栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何将python multiprocessing Process输出发送到Tkinter GUI

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何将python multiprocessing Process输出发送到Tkinter GUI

您可以将stdout /
stderr重定向到myfunc()中的StringIO,然后将写入该StringIO的所有内容发送回父级(如unutbu所建议)。

由于该示例的功能超出了您的需要,因此以下版本更符合您的既定目标:

#!/usr/bin/env pythonimport sysfrom cStringIO import StringIOfrom pre import InteractiveConsolefrom contextlib import contextmanagerfrom multiprocessing import Process, Pipe@contextmanagerdef std_redirector(stdin=sys.stdin, stdout=sys.stdin, stderr=sys.stderr):    tmp_fds = stdin, stdout, stderr    orig_fds = sys.stdin, sys.stdout, sys.stderr    sys.stdin, sys.stdout, sys.stderr = tmp_fds    yield    sys.stdin, sys.stdout, sys.stderr = orig_fdsclass Interpreter(InteractiveConsole):    def __init__(self, locals=None):        InteractiveConsole.__init__(self, locals=locals)        self.output = StringIO()        self.output = StringIO()    def push(self, command):        self.output.reset()        self.output.truncate()        with std_redirector(stdout=self.output, stderr=self.output): try:     more = InteractiveConsole.push(self, command)     result = self.output.getvalue() except (SyntaxError, OverflowError):     pass return more, resultdef myfunc(conn, commands):    output = StringIO()    py = Interpreter()    results = ""    for line in commands.split('n'):        if line and len(line) > 0: more, result = py.push(line + 'n') if result and len(result) > 0:     results += result    conn.send(results)    conn.close()if __name__ == '__main__':    parent_conn, child_conn = Pipe()    commands = """print "[42, None, 'hello']"def greet(name, count):    for i in range(count):        print "Hello, " + name + "!"greet("Beth Cooper", 5)fugaziprint "Still going...""""    p = Process(target=myfunc, args=(child_conn, commands))    p.start()    print parent_conn.recv()    p.join()

有关安全性的通常警告适用于此(即,除非您可以相信这些代码段的发送者不要做任何愚蠢/恶意的操作,否则请不要这样做。)

另请注意,如果您不需要解释python表达式 语句的任意组合,则可以简化很多操作。如果只需要调用生成某些输出的顶级函数,则可能更合适:

def dosomething():    print "Doing something..."def myfunc(conn, command):    output = StringIO()    result = ""    with std_redirector(stdout=output, stderr=output):        try: eval(command) result = output.getvalue()        except Exception, err: result = repr(err)    conn.send(result)    conn.close()if __name__ == '__main__':    parent_conn, child_conn = Pipe()    command = "dosomething()"    p = Process(target=myfunc, args=(child_conn, command))    p.start()    print parent_conn.recv()    p.join()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645096.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号