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

  • SEARCH
  • Home
  • 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 3997676
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:25:51+00:00 2026-05-20T07:25:51+00:00

From within a Python GUI (PyGTK) I start a process (using multiprocessing). The process

  • 0

From within a Python GUI (PyGTK) I start a process (using multiprocessing). The process takes a long time (~20 minutes) to finish. When the process is finished I would like to clean it up (extract the results and join the process). How do I know when the process has finished?

My colleague suggested a busy loop within the parent process that checks if the child process has finished. Surely there is a better way.

In Unix, when a process is forked, a signal handler is called from within the parent process when the child process has finished. But I cannot see anything like that in Python. Am I missing something?

How is it that the end of a child process can be observed from within the parent process? (Of course, I do not want to call Process.join() as it would freeze up the GUI interface.)

This question is not limited to multi-processing: I have exactly the same problem with multi-threading.

  • 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-20T07:25:52+00:00Added an answer on May 20, 2026 at 7:25 am

    This answer is really simple! (It just took me days to work it out.)

    Combined with PyGTK’s idle_add(), you can create an AutoJoiningThread. The total code is borderline trivial:

    class AutoJoiningThread(threading.Thread):
        def run(self):
            threading.Thread.run(self)
            gobject.idle_add(self.join)
    

    If you want to do more than just join (such as collecting results) then you can extend the above class to emit signals on completion, as is done in the following example:

    import threading
    import time
    import sys
    import gobject
    gobject.threads_init()
    
    class Child:
        def __init__(self):
            self.result = None
    
        def play(self, count):
            print "Child starting to play."
            for i in range(count):
                print "Child playing."
                time.sleep(1)
            print "Child finished playing."
            self.result = 42
    
        def get_result(self, obj):
            print "The result was "+str(self.result)
    
    class AutoJoiningThread(threading.Thread, gobject.GObject):
        __gsignals__ = {
            'finished': (gobject.SIGNAL_RUN_LAST,
                         gobject.TYPE_NONE,
                         ())
            }
    
        def __init__(self, *args, **kwargs):
            threading.Thread.__init__(self, *args, **kwargs)
            gobject.GObject.__init__(self)
    
        def run(self):
            threading.Thread.run(self)
            gobject.idle_add(self.join)
            gobject.idle_add(self.emit, 'finished')
    
        def join(self):
            threading.Thread.join(self)
            print "Called Thread.join()"
    
    if __name__ == '__main__':
        print "Creating child"
        child = Child()
        print "Creating thread"
        thread = AutoJoiningThread(target=child.play,
                                   args=(3,))
        thread.connect('finished', child.get_result)
        print "Starting thread"
        thread.start()
        print "Running mainloop (Ctrl+C to exit)"
        mainloop = gobject.MainLoop()
    
        try:
            mainloop.run()
        except KeyboardInterrupt:
            print "Received KeyboardInterrupt.  Quiting."
            sys.exit()
    
        print "God knows how we got here.  Quiting."
        sys.exit()
    

    The output of the above example will depend on the order the threads are executed, but it will be similar to:

    Creating child
    Creating thread
    Starting thread
    Child starting to play.
     Child playing.
    Running mainloop (Ctrl+C to exit)
    Child playing.
    Child playing.
    Child finished playing.
    Called Thread.join()
    The result was 42
    ^CReceived KeyboardInterrupt.  Quiting.

    It’s not possible to create an AutoJoiningProcess in the same way (because we cannot call idle_add() across two different processes), however we can use an AutoJoiningThread to get what we want:

    class AutoJoiningProcess(multiprocessing.Process):
        def start(self):
            thread = AutoJoiningThread(target=self.start_process)
            thread.start() # automatically joins
    
        def start_process(self):
            multiprocessing.Process.start(self)
            self.join()
    

    To demonstrate AutoJoiningProcess here is another example:

    import threading
    import multiprocessing
    import time
    import sys
    import gobject
    gobject.threads_init()
    
    class Child:
        def __init__(self):
            self.result = multiprocessing.Manager().list()
    
        def play(self, count):
            print "Child starting to play."
            for i in range(count):
                print "Child playing."
                time.sleep(1)
        print "Child finished playing."
            self.result.append(42)
    
        def get_result(self, obj):
            print "The result was "+str(self.result)
    
    class AutoJoiningThread(threading.Thread, gobject.GObject):
        __gsignals__ = {
            'finished': (gobject.SIGNAL_RUN_LAST,
                         gobject.TYPE_NONE,
                         ())
        }
    
        def __init__(self, *args, **kwargs):
            threading.Thread.__init__(self, *args, **kwargs)
            gobject.GObject.__init__(self)
    
        def run(self):
            threading.Thread.run(self)
            gobject.idle_add(self.join)
            gobject.idle_add(self.emit, 'finished')
    
        def join(self):
            threading.Thread.join(self)
            print "Called Thread.join()"
    
    class AutoJoiningProcess(multiprocessing.Process, gobject.GObject):
        __gsignals__ = {
            'finished': (gobject.SIGNAL_RUN_LAST,
                         gobject.TYPE_NONE,
                         ())
            }
    
        def __init__(self, *args, **kwargs):
            multiprocessing.Process.__init__(self, *args, **kwargs)
            gobject.GObject.__init__(self)
    
        def start(self):
            thread = AutoJoiningThread(target=self.start_process)
            thread.start()
    
        def start_process(self):
            multiprocessing.Process.start(self)
            self.join()
            gobject.idle_add(self.emit, 'finished')
    
        def join(self):
            multiprocessing.Process.join(self)
            print "Called Process.join()"
    
    if __name__ == '__main__':
        print "Creating child"
        child = Child()
        print "Creating thread"
        process = AutoJoiningProcess(target=child.play,
                                   args=(3,))
        process.connect('finished',child.get_result)
        print "Starting thread"
        process.start()
        print "Running mainloop (Ctrl+C to exit)"
        mainloop = gobject.MainLoop()
    
        try:
            mainloop.run()
        except KeyboardInterrupt:
            print "Received KeyboardInterrupt.  Quiting."
            sys.exit()
    
        print "God knows how we got here.  Quiting."
        sys.exit()
    

    The resulting output will be very similar to the example above, except this time we have both the process joining and it’s attendant thread joining too:

    Creating child
    Creating thread
    Starting thread
    Running mainloop (Ctrl+C to exit)
     Child starting to play.
    Child playing.
    Child playing.
    Child playing.
    Child finished playing.
    Called Process.join()
    The result was [42]
    Called Thread.join()
    ^CReceived KeyboardInterrupt.  Quiting.

    Unfortunately:

    1. This solution is dependent on gobject, due to the use of idle_add(). gobject is used by PyGTK.
    2. This is not a true parent/child relationship. If one of these threads is started by another thread, then it will nonetheless be joined by the thread running the mainloop, not the parent thread. This problem holds true for AutoJoiningProcess too, except there I imagine an exception would be thrown.

    Thus to use this approach, it would be best to only create threads/process from within the mainloop/GUI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to access the as.vector R function from within Python, using rpy2.
From within a python script (main.py), I am using the subprocess module to run
I am running a shell command from within my python script using os.system(some shell
Is there a way to run a jasper report from within python without using
From within my python script, I want to start another python script which will
What is the easiest way to use a DLL file from within Python ?
How can I use Speex to encode/decode from within python? Are there any wrappers?
How do I find out the current runtime directory from within a Python script?
I'm wondering how to remove a dynamic word from a string within Python. It
I want to run a python script from within another. By within I mean

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.