Help with Threading

P

Philip Smith

Hi

I am fairly new to Python threading and my needs are simple(!)

I want to establish a number of threads each of which work on the same
computationally intensive problem in different ways.

I am using the thread module rather than the threading module.

My problem is I can't see how (when one thread completes) to ensure that the
other threads terminate immediately.

Appreciate some simple advice

Phil
 
D

Dennis Lee Bieber

My problem is I can't see how (when one thread completes) to ensure that the
other threads terminate immediately.
I presume your threads contain some sort of loop structure at
some stage...

Insert a non-blocking test on some shared condition variable,
and have the thread terminate /itself/ when the condition is set. The
condition would need to be set, by your code, by whichever thread should
complete first, on its own.

May not even need a condition/event/lock -- a globally shared
variable might be sufficient.

--
 
P

Pierre Barbier de Reuille

Philip Smith a écrit :
Hi

I am fairly new to Python threading and my needs are simple(!)

I want to establish a number of threads each of which work on the same
computationally intensive problem in different ways.

I am using the thread module rather than the threading module.

My problem is I can't see how (when one thread completes) to ensure that the
other threads terminate immediately.

Appreciate some simple advice

Phil

With Python's threads, you have to handle this kindd a feature yourself.
For example, you can create a single object containing a boolean set to
False by default. When one of your algorithm finishes, the boolean is
set to True. All your algorithm should regularly test this boolean and
exit if it is True ! Note that the boolean has to be inside another
object because Boolean types is not mutable. Now, if you really want to
be able to "kill" your threads, you will need another thread interface.
For example, Qt threads allows that ... and WxPython offers you some
functions to do exactly what I described.

Pierre
 

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

Similar Threads


Members online

Forum statistics

Threads
474,218
Messages
2,571,124
Members
47,725
Latest member
Rudy

Latest Threads

Top