Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3437404
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:03:40+00:00 2026-05-18T08:03:40+00:00

I’m trying to get output from a python multiprocessing Process displayed in a Tkinter

  • 0

I’m trying to get output from a python multiprocessing Process displayed in a Tkinter gui.

I can send output from Processes via a gui to a command shell, for example by running the fllowing tiny script at a shell prompt:

from multiprocessing import Process  
import sys  

def myfunc(text):    
    print text  
    sys.stdout.flush() 

def f1():  
    p1 = Process(target = myfunc, args = ("Surprise",))  
    p1.start()  

def f2():  
    p2 = Process(target = myfunc, args = ("Fear",))  
    p2.start()  

def fp():  
    myfunc("... and an almost fanatical devotion to the Pope")  

a = Tk()  

b1 = Button(a, text="Process 1", command=f1)  
b1.grid(row=0, column=0, pady=10, padx=10, sticky=SE)  
b2 = Button(a, text="Process 2", command=f2)  
b2.grid(row=0, column=1, pady=10, padx=10, sticky=SE)  
b3 = Button(a, text="Parent", command=fp)  
b3.grid(row=0, column=2, pady=10, padx=10, sticky=SE)  

if __name__ == "__main__":  
    a.mainloop()

I can also send output from the parent to a Text box, for example by modifying the above by commenting out the flushing of stdout in myfunc

#    sys.stdout.flush()

and adding immediately after the “b3.grid…” line the following:

class STDText(Text):
    def __init__(self, parent, cnf={}, **kw):
        Text.__init__(self, parent, cnf, **kw)
    def write(self, stuff):
        self.config(state=NORMAL)
        self.insert(END, stuff)
        self.yview_pickplace("end")
        self.config(state=DISABLED)

messages = STDText(a, height=2.5, width=30, bg="light cyan", state=DISABLED)   
messages.grid(row=1, column=0, columnspan=3)
sys.stdout = messages

However I can’t figure out how to send output from the Processes to the text box. Am I missing something simple?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:03:40+00:00Added an answer on May 18, 2026 at 8:03 am

    You could redirect stdout/stderr to a StringIO in myfunc(), then send whatever gets written into that StringIO back to the parent (as suggested by unutbu). See my answer to this question for one way of doing this redirection.

    Since that example does a bit more than you need, here’s a version that’s more aligned with your stated goals:

    #!/usr/bin/env python
    import sys
    from cStringIO import StringIO
    from code import InteractiveConsole
    from contextlib import contextmanager
    from multiprocessing import Process, Pipe
    
    @contextmanager
    def 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_fds
    
    class 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, result
    
    def 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)
    fugazi
    print "Still going..."
    """
        p = Process(target=myfunc, args=(child_conn, commands))
        p.start()
        print parent_conn.recv()
        p.join()
    

    The usual caveats about security apply here (i.e., don’t do this unless you can trust the sender of these code snippets to not do anything stupid/malicious).

    Also note that you can simplify this a lot if you don’t need to interpret an arbitrary mix of python expressions and statements. If you only need to call a top-level function that generates some outputs, something like this may be more appropriate:

    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()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.