Python share CPU time?

Grant Edwards grante at visi.com
Wed Aug 9 15:08:47 EDT 2006


On 2006-08-09, Tim Chase <python.list at tim.thechases.com> wrote:

>> Problem is that I would have to share the CPU between all the robots,
>> and thus allocate a time period to each robot. However I couldn't find
>> any way to start a thread (robot), and interrupt it after a given time
>> period.
>> Any suggestions on how to proceed?

> >>> import thread, time
> >>> def robot(name):
> ...     for i in xrange(5):
> ...             print "%s on pass %i" % (name, i)
> ...             time.sleep(0.01)

>[...]

I originally suggested the thread/sleep() scheme, but after
thinking about it more, I really like the generator approach in
the case where you want to explicity end each robot's "turn"
using the CPU (which is what the sleep() call does).

OTOH, if you want the robots to wait for events of some sort
(e.g. from a queue) or data from an I/O device, then threading
is probably the way to go.

I guess it all depends on whether the robots' behavior is to be
time-driven or event-driven.

-- 
Grant Edwards                   grante             Yow!  Used staples are good
                                  at               with SOY SAUCE!
                               visi.com            



More information about the Python-list mailing list