[Tutor] Is generator function similar to multi threading?

2007-03-16 Thread ammar azif
Is generator function similar to multi threading?



 
-
Don't be flakey. Get Yahoo! Mail for Mobile and 
always stay connected to friends.___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is generator function similar to multi threading?

2007-03-16 Thread Kent Johnson
ammar azif wrote:
 Is generator function similar to multi threading?

Only in that they can be alternative ways to solve the same problem.

Generator functions are similar to co-routines and can be used to create 
multiple lightweight tasks. SimPy (http://simpy.sourceforge.net/) is an 
example of this.

However generator functions are most commonly used as a convenient way 
to generate a sequence when the generation involves some saved state.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Is generator function similar to multi threading?

2007-03-16 Thread Michael Sparks
On Friday 16 March 2007 06:52, ammar azif wrote:
 Is generator function similar to multi threading?

Yes, however unlike threads they are more general. They can be used in two 
main ways:
   * To generate a sequence of values
   * To achieve something similar to multithreading, but without using
 operating system threads.

For a tutorial on how to do the latter safely, please take a look at this:
   http://kamaelia.sourceforge.net/MiniAxon/

(It's targeted at someone with very basic python skills, since the first 
iteration of that tutorial was written for someone who'd learnt python the 
previous week)

We use the latter form extremely heavily on our project to great effect. One 
of benefits of using a generator over a thread is that you can (as of python 
2.5) kill a generator by sending an exception into it.(though this isn't the 
reason we're using them) 

As a result combining threading and generators makes sense since you can run a 
generator inside a thread, and have a mechanism to kill the thread - you send 
an exception into the generator.

That said, the primary intended use of generators *is* to generate values. The 
fact they're a restricted co-routine however is incredibly handy. (Because 
they can only be single layer, it forces them to be simpler, which in turn 
makes them much more reusable)

Regards,


Michael.
--
http://kamaelia.sourceforge.net/Home
http://yeoldeclue.com/blog
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor