473,381 Members | 1,402 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

wxPython and threading issue

Hi,
I'm hoping someone here will be able to help as I've been struggling with
this problem for a few days now.

I'm working on an application that is creating a ProgressDialog, and then
creating a thread that runs a function from another module in the program.

The problem is, when the cancel button on the ProgressDialog is pressed, the
thread that was created continues to run. How can I make it so that when
the cancel button on the dialog is clicked, the spawned thread dies?

thanks
Sep 29 '06 #1
8 1666
Patrick Smith wrote:
Hi,
I'm hoping someone here will be able to help as I've been struggling with
this problem for a few days now.

I'm working on an application that is creating a ProgressDialog, and then
creating a thread that runs a function from another module in the program.

The problem is, when the cancel button on the ProgressDialog is pressed, the
thread that was created continues to run. How can I make it so that when
the cancel button on the dialog is clicked, the spawned thread dies?
Have the main thread set a flag telling the worker thread to exit, and
have the worker thread check that periodically when it knows it's in a
safe state to exit.

Sep 29 '06 #2
Hi,
Thanks for your reply.

<sj*******@yahoo.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
Patrick Smith wrote:
Hi,
I'm hoping someone here will be able to help as I've been struggling
with
this problem for a few days now.

I'm working on an application that is creating a ProgressDialog, and
then
creating a thread that runs a function from another module in the
program.

The problem is, when the cancel button on the ProgressDialog is pressed,
the
thread that was created continues to run. How can I make it so that
when
the cancel button on the dialog is clicked, the spawned thread dies?

Have the main thread set a flag telling the worker thread to exit, and
have the worker thread check that periodically when it knows it's in a
safe state to exit.
This would work, unfortunately, the thread that it spawns calls a function
in a loop, that function has an incredibly long run-time, on the order of
minutes (possibly hours depending on the input), and that function, its self
is multithreaded.
This means that the worker thread could only check the flag after each
completion of this long-running function.

Given that this is the situation, is it possible to do what I mentioned
above? Or does the long running function prevent any nice way of doing
this?
Sep 29 '06 #3
Patrick Smith wrote:
Hi,
Thanks for your reply.

<sj*******@yahoo.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
>>Patrick Smith wrote:
>>>Hi,
I'm hoping someone here will be able to help as I've been struggling

with
>>>this problem for a few days now.

I'm working on an application that is creating a ProgressDialog, and

then
>>>creating a thread that runs a function from another module in the

program.
>>>The problem is, when the cancel button on the ProgressDialog is pressed,

the
>>>thread that was created continues to run. How can I make it so that

when
>>>the cancel button on the dialog is clicked, the spawned thread dies?

Have the main thread set a flag telling the worker thread to exit, and
have the worker thread check that periodically when it knows it's in a
safe state to exit.


This would work, unfortunately, the thread that it spawns calls a function
in a loop, that function has an incredibly long run-time, on the order of
minutes (possibly hours depending on the input), and that function, its self
is multithreaded.
This means that the worker thread could only check the flag after each
completion of this long-running function.

Given that this is the situation, is it possible to do what I mentioned
above? Or does the long running function prevent any nice way of doing
this?

If the function is a black box then you are stymied, I suspect.

Had you considered using a sub-process instead of a thread to perform
the lengthy computation?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Sep 29 '06 #4
Patrick Smith wrote:
Hi,
Thanks for your reply.

<sj*******@yahoo.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
[Re: cancelling a worker thread from the GUI thread]

Have the main thread set a flag telling the worker thread to exit, and
have the worker thread check that periodically when it knows it's in a
safe state to exit.

This would work, unfortunately, the thread that it spawns calls a function
in a loop, that function has an incredibly long run-time, on the order of
minutes (possibly hours depending on the input), and that function, its self
is multithreaded.
This means that the worker thread could only check the flag after each
completion of this long-running function.

Given that this is the situation, is it possible to do what I mentioned
above? Or does the long running function prevent any nice way of doing
this?
Well, the problem is that you can't simply kill a thread--it shares
memory with other threads that it could be leaving in an inconsistent
state. Imagine that it was, say, holding a lock when it was forceably
killed. Now any other thread that tries to acquire that lock will
block forever.

You really do need the thread's cooperation so that it only exits when
everything is in a kosher state. Can you dig into the incredibly long
function and change it to do the flag-checking and exiting safely?

I second the notion to consider using subprocesses instead of threads;
that's almost always a good idea unless you're really sharing a lot of
complex data structures.

Sep 29 '06 #5
If your thread is long running and it is not possible to easily set a
flag for it to check and bail out, then how does it display the
progress in the progress dialog. How often does that get updated? If
the progress dialog is updated often, then at each update have the
thread check a self.please_die flag, for example, and break out
(possibly after releasing some resources...).
Having the work done in an external process might or might not work
easily for your problem. It will make it easy to kill the execution but
it might be OS dependent, and you will have to define some
communication interface between your processes (for example, how would
an external process easily provide progress feedback to its parent?).
Patrick Smith wrote:
Hi,
I'm hoping someone here will be able to help as I've been struggling with
this problem for a few days now.

I'm working on an application that is creating a ProgressDialog, and then
creating a thread that runs a function from another module in the program.

The problem is, when the cancel button on the ProgressDialog is pressed, the
thread that was created continues to run. How can I make it so that when
the cancel button on the dialog is clicked, the spawned thread dies?

thanks
Sep 29 '06 #6
Well, the problem is that you can't simply kill a thread--it shares
memory with other threads that it could be leaving in an inconsistent
state. Imagine that it was, say, holding a lock when it was forceably
killed. Now any other thread that tries to acquire that lock will
block forever.

You really do need the thread's cooperation so that it only exits when
everything is in a kosher state. Can you dig into the incredibly long
function and change it to do the flag-checking and exiting safely?

I second the notion to consider using subprocesses instead of threads;
that's almost always a good idea unless you're really sharing a lot of
complex data structures.
Hi,
Thanks again for your replies.

The way this was originally working was actually using a subprocess,
however, the people who this program is for were having problems that when
the application was closed, the subprocess was still around in the system,
which was why they wanted the other module it is using to have a function it
could call, rather then using popen on the main method.

Also, the people who I am doing this for need something ASAP and for now are
fine with the cancel button completely closing the entire program. Is there
at least a nice way to do this?

Thanks for all your replies,they've all been very helpful.
Sep 29 '06 #7

"Nick Vatamaniuc" <va******@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
If your thread is long running and it is not possible to easily set a
flag for it to check and bail out, then how does it display the
progress in the progress dialog. How often does that get updated? If
the progress dialog is updated often, then at each update have the
thread check a self.please_die flag, for example, and break out
(possibly after releasing some resources...).
Having the work done in an external process might or might not work
easily for your problem. It will make it easy to kill the execution but
it might be OS dependent, and you will have to define some
communication interface between your processes (for example, how would
an external process easily provide progress feedback to its parent?).

Hi,
The external process was their original approach, but when cancel was
pressed or the UI was closed they found the subprocess still around. Also,
it must work on windows, linux, and mac.

thanks.
Sep 29 '06 #8
Patrick Smith wrote:
Well, the problem is that you can't simply kill a thread--it shares
memory with other threads that it could be leaving in an inconsistent
state. Imagine that it was, say, holding a lock when it was forceably
killed. Now any other thread that tries to acquire that lock will
block forever.

You really do need the thread's cooperation so that it only exits when
everything is in a kosher state. Can you dig into the incredibly long
function and change it to do the flag-checking and exiting safely?

I second the notion to consider using subprocesses instead of threads;
that's almost always a good idea unless you're really sharing a lot of
complex data structures.

Hi,
Thanks again for your replies.

The way this was originally working was actually using a subprocess,
however, the people who this program is for were having problems that when
the application was closed, the subprocess was still around in the system,
Have the main process kill the subprocess when it exits; this should be
fine unless you use kill -9 or the windows equivalent (e.g terminating
from the task manager), in which case you can kill the subproc manually
as well.
Also, the people who I am doing this for need something ASAP and for now are
fine with the cancel button completely closing the entire program. Is there
at least a nice way to do this?
maybe os.abort or os._exit depending on what you want and how they
behave on your platform?

Sep 29 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Grant Edwards | last post by:
Can anybody recommend a good book on wxPython? Are there any books on wxPython? I've been trying to learn wxPython and/or wax for a few weeks, and I'm just not getting it. wxWindows seems...
3
by: USCode | last post by:
I've been poking around the wxPython website and am unclear if wxPython has a wrapper for ALL wxWidget classes or just the GUI-related classes? Does wxPython also provide a python wrapper for the...
8
by: Erik Johnson | last post by:
I am looking for some input on GUI libraries. I want to build a Python-driven GUI, but don't really understand the playing field very well. I have generally heard good things about wxPython. I...
2
by: Daniel Bickett | last post by:
Hello, I am writing an application using two event-driven libraries: wxPython, and twisted. The first problem I encountered in the program is the confliction between the two all-consuming...
5
by: fooooo | last post by:
This is a network app, written in wxPython and the socket module. This is what I want to happen: GUI app starts. User clicks a button to 'start' the work of the app. When start is pressed, a new...
25
by: TPJ | last post by:
GUI's etc: PyGtk on Windows "(...) So if someone develops mainly for X and just wants to make sure that it is not impossible to run on Windows, you can use PyGTK. (...)", July 2nd, 1999 pyGTK...
2
by: ccahoon | last post by:
In wxPython, I want to be able to start downloading a file and have the window doing such remain interactive, so that the user can cancel downloading the next file. Also, if there is a way to...
9
by: zxo102 | last post by:
Hi everyone, I am using a python socket server to collect data from a socket client and then control a image location ( wxpython) with the data, i.e. moving the image around in the wxpython frame....
1
by: [david] | last post by:
What am I doing wrong? I'm trying to capture stdErr in a multi-threaded program. This code crashes wxPython with /Py Assertion Error: C++ assertion "m_count=-1 || m_count=-2" failed/ What I'm...
3
by: Shafik | last post by:
Hello folks, I'm having an issue with mixing wxPython and threading ... I realize multi-threading always introduces subtle bugs, but the following scenario is just odd: I start a dummy...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.