#!/usr/bin/python
#This file was automatically generated by spin from a .pyy file.

# Run this file through "spin" to get a .py file.

import Yarn
from Yarn import simtime
import random

def trylickin(thing):
	print "%f: I'm gonna try lickin' %s"%(simtime(), thing)
	for i in range(1,21):
		r = random.uniform(1, 2)
		SPINTMP=Yarn.Yarn(Yarn.sleep(r))
		yield (2, SPINTMP)
		if SPINTMP.resumeWhen:
			raise SPINTMP.resumeWhen
		print "%f: Lick %d of %s took %f seconds..."%(simtime(),i,thing,r)
		if random.uniform(0, 10) < 1:
			print "%f: But that was enough, I'm satisfied!"%(simtime())
			yield (0, i)
		if random.uniform(0, 10) < 1:
			print "%f: But... gasp... poison!"%(simtime())
			raise Exception, thing


def reportLicks(thing):
	try:
		SPINTMP=Yarn.Yarn(trylickin(thing))
		yield (2, SPINTMP)
		if SPINTMP.resumeWhen:
			raise SPINTMP.resumeWhen
		tries  = SPINTMP.returnTo
	except Exception, e:
		print "%f: Lickin' tester of %s DIED! (exception=%s)"%(simtime(), thing, e)
		print "%f: Starting over..."%(simtime())
		Yarn.spawn(reportLicks(thing))
	else:
		if tries:
			print "%f: Lickin' tester reports back %d tries for %s"%(simtime(), tries, thing)
		else:
			print "%f: Lickin' tester didn't report # of tries for %s!"%(simtime(), thing)

Yarn.spawn(reportLicks("Tootsie Roll Pop"))
Yarn.spawnAt(5.,reportLicks("Rover"))

Yarn.run()


