1

I have a python code that creates a GUI using wx that allows the user to pick a file on their computer and then calls a C function that I wrapped with swig that uses this file and does calculations and saves it to a new file. I want this function call to run in one thread, while the wx window won't stop responding and I also have a loading bar that estimates the amount of time it will take to finish the calculations and updates the progress every second. Currently, the code starts running the loading bar function in a thread, then starts running the calculations in a thread, but this causes the loading bar function to pause. After all of the calculations are done a few minutes later the loading bar function resumes. I want these threads to run in parallel, but I cant get it to work with this wrapped function (other times I've used this threading it has worked).

Part of my code is as follows:

thread.start_new_thread(self.loading_bar, (max_count,))
thread.start_new_thread(self.run_interpolation, (option,))

def loading_bar(self, max_count):
    loaded = 1.0
    print 180 + (max_count / 2), loaded
    while loaded < 100.0:
        print 180 + (max_count / 2), loaded
        sleep(1)
        wx.CallAfter(self.gauge.SetValue, loaded)
        loaded += 100.0 / (180 + (max_count / 2))
    sleep(2)
    wx.CallAfter(self.gauge.SetValue, 0)

def run_interpolation(self, option):
    weather_interp.main_func(self.input[1], self.input[2], self.location, option)

I have heard that the multiprocessing modules, and threading modules are better than the thread module, but I don't understand why and trying to use them didn't seem to work either.

2 Answers 2

2

You might want to use the threading library rather than the low-level thread library. Since your current task isn't processor-bound, you don't really need to take advantage of multiple processors or multiple cores, so I wouldn't worry about multiprocessing -- you need multiple Python threads, but you don't need multiple OS threads.

A simple example using the threading library:

#!/usr/bin/env python
import threading
import time

def foo():
  print 'foo 1'
  time.sleep(0.2)
  print 'foo 2'

def bar():
  time.sleep(0.1)
  print 'bar 1'
  time.sleep(0.2)
  print 'bar 2'

threading.Thread(target=foo).start()
threading.Thread(target=bar).start()

This should print

foo 1
bar 1
foo 2
bar 2

So you know that it's interleaved and that threading is working. Based on your code snippet, it looks like your threading stuff is fine.

As a result, I suspect that the problem lies in the CallAfter call. According to its documentation, CallAfter runs after the current event handler has exited. Based on your snippet, it doesn't seem like you're exiting the event handler. Perhaps you want to directly set the gauge's value in the loop?

Sign up to request clarification or add additional context in comments.

Comments

1

The problem was the c wrapped function I was calling. To fix this problem I added a -threads flag to swig when compiling the interface file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.