WINXP vs. LINUX in threading.Thread

K

Kent

hello all,

i want to add a "new update notification" feature to my wxPython appl.
The codes below do the job. The logic is simple enough, I don't think
it needs to be explained.

since sometimes, under windows, proxy setting was a script. and was
set in IE. In this case, connecting to the HTML will take relative
long time. I therefore run the following codes in a new Thread
(subclass of threading.Thread), so that user don't have to wait during
the version checking period.

Under Linux, it worked smoothly. But under Windows XP, it didn't. If
there was new updates, the notification dialog can show, but no text,
icon, .... on it. Then, the whole application didn't response any
longer. :( I have to force stop the application process.

where is the problem?

Thanks.

code to get the version from a HTML:

def getLatestVersion(versionUrl):
#proxy is only for testing
# proxy_support = urllib2.ProxyHandler
({"http":"10.48.187.80:8080"})
# opener = urllib2.build_opener(proxy_support)
# urllib2.install_opener(opener)
#proxy is only for testing

result = ''
try:
f = urllib2.urlopen(versionUrl)
s = f.read()
f.close()
result = s.split("#xxx#")[1]
except:
pass
return result

Thread class:

class UpdateChecker(Thread):
def __init__(self):
Thread.__init__(self)

def run(self):
lv = util.getLatestVersion(config.VERSION_URL)
if lv>config.VERSION:
showUpdateDialog(lv)

codes to start the thread:

updatechk = UpdateChecker()
updatechk.start()
 
D

Diez B. Roggisch

Kent said:
hello all,

i want to add a "new update notification" feature to my wxPython appl.
The codes below do the job. The logic is simple enough, I don't think
it needs to be explained.

since sometimes, under windows, proxy setting was a script. and was
set in IE. In this case, connecting to the HTML will take relative
long time. I therefore run the following codes in a new Thread
(subclass of threading.Thread), so that user don't have to wait during
the version checking period.

Under Linux, it worked smoothly. But under Windows XP, it didn't. If
there was new updates, the notification dialog can show, but no text,
icon, .... on it. Then, the whole application didn't response any
longer. :( I have to force stop the application process.

where is the problem?

GUI-toolkits and threads usually are not a good idea (Qt4 being an
exception to that rule, at least they claim that). Google wxPython +
threading for answers how to solve this - essentially, you need to
create a timer or event-based solution that allows your
background-thread to inject a status message to the main eventloop.

Diez
 
L

Lie Ryan

Diez said:
GUI-toolkits and threads usually are not a good idea (Qt4 being an
exception to that rule, at least they claim that). Google wxPython +
threading for answers how to solve this - essentially, you need to
create a timer or event-based solution that allows your
background-thread to inject a status message to the main eventloop.

Diez

You should use the GUI toolkit's own event loop for threading. An event
loop is some sort of cooperative multithreading mechanism, if you used
the threading mechanism from the threading library, there will be two
conflicting threading mechanism, resulting in many hard-to-predict
situation if you're not careful. The wxYield mechanism is much easier
option than multithreading.
 
C

Carl Banks

You should use the GUI toolkit's own event loop for threading. An event
loop is some sort of cooperative multithreading mechanism,

The function in question, gethostbyname, isn't cooperating, though.
It blocks for a substantial amount of time from an event handler,
freezing the application.
if you used
the threading mechanism from the threading library, there will be two
conflicting threading mechanism, resulting in many hard-to-predict
situation if you're not careful. The wxYield mechanism is much easier
option than multithreading.

The problem is, during most of the delay wxYield can't be called
becaust the function gethostbyname is blocking.

I think Diez is correct. To avoid the freeze, one should spawn a
thread, and when it completes it should notify the GUI thread by
pushing an event or scheduling an idle call. Functions that do that
are usually thread-safe. (A permanent worker thread might be better
but it would involve a lot more synchronization.)

Carl Banks
 
K

Kent

Thanx you guys.

Now my program is working.

I used the Thread subclass. and at the end of the run method, i call
wx.CallAfter(mainFrame.somefunction, para) to show the dialog or
change some text. I tested in winxp&linux. both worked.


Kent
 
M

Mike Driscoll

hello all,

i want to add a "new update notification" feature to my wxPython appl.
The codes below do the job. The logic is simple enough, I don't think
it needs to be explained.

since sometimes, under windows, proxy setting was a script. and was
set in IE. In this case, connecting to the HTML will take relative
long time. I therefore run the following codes in a new Thread
(subclass of threading.Thread), so that user don't have to wait during
the version checking period.

Under Linux, it worked smoothly. But under Windows XP, it didn't. If
there was new updates, the notification dialog can show, but no text,
icon, .... on it. Then, the whole application didn't response any
longer. :( I have to force stop the application process.

where is the problem?

Thanks.

code to get the version from a HTML:

def getLatestVersion(versionUrl):
    #proxy is only for testing
#    proxy_support = urllib2.ProxyHandler
({"http":"10.48.187.80:8080"})
#    opener = urllib2.build_opener(proxy_support)
#    urllib2.install_opener(opener)
    #proxy is only for testing

    result = ''
    try:
        f = urllib2.urlopen(versionUrl)
        s = f.read()
        f.close()
        result = s.split("#xxx#")[1]
    except:
        pass
    return result

Thread class:

class UpdateChecker(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        lv = util.getLatestVersion(config.VERSION_URL)
        if lv>config.VERSION:
            showUpdateDialog(lv)

codes to start the thread:

updatechk = UpdateChecker()
updatechk.start()

You are probably blocking wxPython's main loop. There are several
examples of using threads with wxPython on their wiki:
http://wiki.wxpython.org/LongRunningTasks

As you will find, there are various methods within wxPython that are
threadsafe (such as wx.CallAfter) and you can use those to communicate
with and update your GUI. From what I've read, this is normal across
GUI toolkits. They all have special ways to work with threads.

In the future, I highly recommend that you join and post to the
wxPython list as that's where you'll get the best targeted advice (see
their website). This list is great for general questions though.

- Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top