943,812 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1234
  • Python RSS
Jul 10th, 2009
0

Can't update wxPython UI during subprocess

Expand Post »
I'm making a simple wxPython app on win32 that does a number of calls to external binaries. Everything works fine, except the GUI doesn't update well while the external process is running. This makes for an ugly window and non-updated status StaticTexts, so I'd like to fix it. Window . Update () helps some, but it still doesn't do the job. I've also tried Window . Refresh () and Window . UpdateWindowUI () and every combination of the three, but no luck. Any thoughts?

Python Syntax (Toggle Plain Text)
  1. # Call process
  2. Process = subprocess . Popen ( Command_Line )
  3. # Wait on process to complete
  4. while Process . poll () == None :
  5. # Update GUI
  6. Window . Update ()
  7. # Wait a bit, don't hog the CPU
  8. time . sleep ( 0.25 )
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SpiritGeek is offline Offline
7 posts
since Jul 2009
Jul 10th, 2009
0

Re: Can't update wxPython UI during subprocess

You'll need to use threading to handle this. One thread calls the function which does the subprocess, and the other calls a function which updates the GUI. Something along the lines of:
python Syntax (Toggle Plain Text)
  1. # untested!
  2. import thread
  3. from time import sleep
  4.  
  5. def myFunc():
  6. while True:
  7. print 'Hello world!'
  8. sleep(1)
  9. def func2():
  10. while True:
  11. print '#2!'
  12. sleep(0.5)
  13.  
  14. thread.start_new_thread(myFunc, ())
  15. thread.start_new_thread(func2, ())
It's started running the myFunc function's endless loop and func2's stuff while it can continue on with other things in the script. Check this for more info:

http://docs.python.org/library/thread.html

Also, please don't use such bad writing style in your code. Only put spaces between non-unary operators. Or, at least don't put spaces between the periods denoting namespaces (like "time . sleep(0.25)") and the colons in your conditional statements. You can check here for a style guide for Python.
Last edited by shadwickman; Jul 10th, 2009 at 6:53 pm.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 10th, 2009
0

Re: Can't update wxPython UI during subprocess

Thanks for the suggestion!

I needed a way to wait on the thread to complete before continuing, so I wound up using threading instead. However, it's still not updating the window well.

Python Syntax (Toggle Plain Text)
  1. import subprocess
  2. import threading
  3. import time
  4.  
  5. class Thread ( threading . Thread ):
  6. def run ( Self ):
  7. subprocess . call ( "mycommand.exe" )
  8.  
  9. External_Executable = Thread ()
  10. External_Executable . start ()
  11.  
  12. while External_Executable . is_alive ():
  13. Window . Update ()
  14. time . sleep ( 0.25 )

With this setup, it's acting just the way it did before. I'd appreciate any more feedback anyone might have.

Thanks!


P.S. My coding style is on purpose. I put great emphasis on human readability, which comes out in many ways, including generous whitespace.
Last edited by SpiritGeek; Jul 10th, 2009 at 7:54 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SpiritGeek is offline Offline
7 posts
since Jul 2009
Jul 10th, 2009
0

Re: Can't update wxPython UI during subprocess

I'd put the window updating segment in a function and call that via a thread as well. Try how that works out.

P.S. As long as your style works for you. I personally can't stand it as whitespace is good, but too much is difficult. It just makes it hard to see some of the OOP-based concepts with the periods being so spaced out.
Reputation Points: 186
Solved Threads: 77
Posting Pro in Training
shadwickman is offline Offline
495 posts
since Jul 2007
Jul 10th, 2009
1

Re: Can't update wxPython UI during subprocess

Your code has a couple of issues, if i am not wrong it will not actually start using threading with your code as it is. You need to add a couple of lines
python Syntax (Toggle Plain Text)
  1. class Thread ( threading . Thread ):
  2. def __init__(self):
  3. threading.Thread.__init__(self)
  4.  
  5. def run ( Self ):
  6. subprocess . call ( "mycommand.exe" )
See how i have called the __init__ method on the threading.Thread class? This means that now it will run the __init__ method of the Thread class, which is needed for threading to work properly with classes.

So try replacing you current class with that and try again and see how it all goes

Hope that helps
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Jul 11th, 2009
0

Re: Can't update wxPython UI during subprocess

Well, based on the replies here, I just spent the last two days teaching myself the multiprocessing module (with inter-process queues) so I could put the UI in one process and the workload in another to solve this problem.

I was pretty much done with that when DevCoach at Dev Shed offered this: instead of Window . Update (), use wx . Yield ().

It works perfectly.

Well, at least I know how to multi-process now! LOL

Thanks, all!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SpiritGeek is offline Offline
7 posts
since Jul 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: clonedigger installation
Next Thread in Python Forum Timeline: Encoding Issues





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC