Can't update wxPython UI during subprocess

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 7
Reputation: SpiritGeek is an unknown quantity at this point 
Solved Threads: 0
SpiritGeek SpiritGeek is offline Offline
Newbie Poster

Can't update wxPython UI during subprocess

 
0
  #1
Jul 10th, 2009
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?

  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 )
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Can't update wxPython UI during subprocess

 
0
  #2
Jul 10th, 2009
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:
  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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 7
Reputation: SpiritGeek is an unknown quantity at this point 
Solved Threads: 0
SpiritGeek SpiritGeek is offline Offline
Newbie Poster

Re: Can't update wxPython UI during subprocess

 
0
  #3
Jul 10th, 2009
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 489
Reputation: shadwickman will become famous soon enough shadwickman will become famous soon enough 
Solved Threads: 76
shadwickman's Avatar
shadwickman shadwickman is offline Offline
Posting Pro in Training

Re: Can't update wxPython UI during subprocess

 
0
  #4
Jul 10th, 2009
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.
"Two good old boys in a fire-apple red convertible. Stoned. Ripped. Twisted. Good people."
- Hunter S. Thompson

my photography
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 950
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 146
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Can't update wxPython UI during subprocess

 
1
  #5
Jul 10th, 2009
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
  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
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 7
Reputation: SpiritGeek is an unknown quantity at this point 
Solved Threads: 0
SpiritGeek SpiritGeek is offline Offline
Newbie Poster

Re: Can't update wxPython UI during subprocess

 
0
  #6
Jul 11th, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 480 | Replies: 5
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC