Simple example involving threads and infinite loops.

For those that happen to have downloaded Crunchy (version 0.9x), I have posted a very simple example[1] that illustrate some properties of threads, and possible interactions between them. http://crunchypython.blogspot.com/2007/07/unending-loops-and-magic-of-thread... The idea of threads is something that should be accessible to anyone familiar with using a computer. However, I can not think of a simpler example in any Python environment to demonstrate 1. How threads can allow different parts of a program to work independently - in this case, two different Python interpreters. 2. How interactions between threads make certain things (like breaking of an infinite loop) possible which would not be using a single thread. Of course, the user has to take the teacher's word (or mine) for it when it is stated that the two Python interpreters are running in separate threads. André [1]... and, as is often the case, this example has uncovered a "small" bug with Crunchy which you might get to see...

2. How interactions between threads make certain things (like breaking of an infinite loop) possible which would not be using a single thread.
This reminds me to add that we looked at the infinite loop in my Python slides regarding icosahedral numbers. I reinforce how the "while True" construct, with no opportunities for breaking out, might be an OK construct where *generators* are concerned. As teachers, we need to dilute some of that prejudice against 'while true' loops, lest we unwittingly pass it on to new talent. Kirby

On 7/17/07, kirby urner <kirby.urner@gmail.com> wrote:
2. How interactions between threads make certain things (like breaking of an infinite loop) possible which would not be using a single thread.
This reminds me to add that we looked at the infinite loop in my Python slides regarding icosahedral numbers.
I reinforce how the "while True" construct, with no opportunities for breaking out, might be an OK construct where *generators* are concerned.
Or web servers! ;-) (Then again, it's always useful to think of having the possibility of ending a program gracefully, rather than having to use the power off button...) André
As teachers, we need to dilute some of that prejudice against 'while true' loops, lest we unwittingly pass it on to new talent.
Kirby _______________________________________________ Edu-sig mailing list Edu-sig@python.org http://mail.python.org/mailman/listinfo/edu-sig

I reinforce how the "while True" construct, with no opportunities for breaking out, might be an OK construct where *generators* are concerned.
Or web servers! ;-)
Yeah, very true. Daemons of all kinds are like infinite loops, connected to Windows TSRs and services in this article: http://en.wikipedia.org/wiki/Daemon_(computer_software)
(Then again, it's always useful to think of having the possibility of ending a program gracefully, rather than having to use the power off button...)
André
What's cool about a generator is it stops execution at the yield, so there's no penalty for not writing exit code. A generator gets garbage collected just like any other object, without needing to be explicitly halted or paused. Kirby

Following some bug hunting, I have written an update on the blog post mentioned in my previous message. I think it shows very clearly how the borg interpreters do indeed share a common state - and how unexpected result can happen when working with communicating threads (even those that were supposed to properly use lock and release...). I'd be interested in getting comments from those that try it out. André On 7/17/07, Andre Roberge <andre.roberge@gmail.com> wrote:
For those that happen to have downloaded Crunchy (version 0.9x), I have posted a very simple example[1] that illustrate some properties of threads, and possible interactions between them.
http://crunchypython.blogspot.com/2007/07/unending-loops-and-magic-of-thread...
The idea of threads is something that should be accessible to anyone familiar with using a computer. However, I can not think of a simpler example in any Python environment to demonstrate 1. How threads can allow different parts of a program to work independently - in this case, two different Python interpreters. 2. How interactions between threads make certain things (like breaking of an infinite loop) possible which would not be using a single thread.
Of course, the user has to take the teacher's word (or mine) for it when it is stated that the two Python interpreters are running in separate threads.
André
[1]... and, as is often the case, this example has uncovered a "small" bug with Crunchy which you might get to see...
participants (2)
-
Andre Roberge
-
kirby urner