[Stackless] Starting in stackless...

Carlos Eduardo de Paula cedepaula at yahoo.com.br
Thu Mar 23 14:57:00 CET 2006


If you uncomment the portion:

#        if ((len(queue) <= 5) and
(producers[len(producers)-1].blocked)):
#            ch_c.receive()
#            ch_p.send(None)

inside the consumer loop, it would awake the producer
at that situation....

Carlos


--- Carlos Eduardo de Paula <cedepaula at yahoo.com.br>
escreveu:

> I updated my code a little.. cleaned some things...
> but I have one doubt... do I need 2 channels... one
> for the producer and one for the consumer to control
> the chain? Only one channel wouldn´t take care of it
> all?
> 
> Thanks,
> 
> Carlos
> 
> ------------------ Code ------------------
> 
> import stackless
> import time
> 
> # This is one way to do a time delay in Stackless
> while allowing
> # other tasklets to run.
> def delay(seconds):
>      startTime = time.clock()
>      stopTime = startTime + seconds
>      while time.clock() < stopTime:
>          stackless.schedule()
> 
> # The other way to put the tasklet to sleep - from
> stackless.com wiki/Idioms
>
##########################################################
> sleepingTasklets = []
> 
> def Sleep(secondsToWait):
>     channel = stackless.channel()
>     endTime = time.time() + secondsToWait
>     sleepingTasklets.append((endTime, channel))
>     sleepingTasklets.sort()
>     # Block until we get sent an awakening
> notification.
>     channel.receive()
> 
> def ManageSleepingTasklets():
>     while 1:
>         if len(sleepingTasklets):
>             endTime = sleepingTasklets[0][0]
>             if endTime <= time.time():
>                 channel = sleepingTasklets[0][1]
>                 del sleepingTasklets[0]
>                 # We have to send something, but it
> doesn't matter what as it is not used.
>                 channel.send(None)
>         stackless.schedule()
> 
> stackless.tasklet(ManageSleepingTasklets)()
> 
>
##########################################################
> 
> def printStatus(reporter):
>      print reporter + "[" + "#" * len(queue) + " " *
> (q_size-len(queue))  + "] Qty:" , len(queue) , "\r",
>      time.sleep(0.1)  # so we have time to see the
> displayed data
>      # keep in mind that this call to time.sleep
> will
> stall
>      # all tasklets until time.sleep returns
> 
> def producer(who):
>      while True:
>          if (len(queue) < q_size):
>              queue.append("a")
>              p_counter[int(who)] += 1
>              printStatus('P'+who)
>              delay(0.1)
> #             Sleep(0.2)
>          else:
>              ch_c.send(None)        
>              ch_p.receive()
> 
> def consumer(who):
>      while True:
>         # Check if the producer is blocked and the
> queue is low, then wake the producer
> #        if ((len(queue) <= 5) and
> (producers[len(producers)-1].blocked)):
> #            ch_c.receive()
> #            ch_p.send(None)
>         if (len(queue) >= 1):
>             queue.pop()
>             c_counter[int(who)] += 1
>             printStatus('C'+who)
>             delay(0.8)
> #            Sleep(0.8)
>         else:
>             # This is a breeding ground for
> deadlocks.
>             # In this case the producer has almost
> definitely
>             # called ch_c.send and is blocked.
>             # if you call ch_p.send first then both
>             # tasklets will be blocked for all
> eternity
>             # and stackless.run will return
>             ch_c.receive()
>             ch_p.send(None)
> 
> 
> def launch_p (ind):         # Launches and
> initializes
> the producers lists
>     producers.append(int(ind))
>     p_counter.append(0)
>     producers[int(ind)] =
> stackless.tasklet(producer)(ind)
> 
> def launch_c (ind):         # Launches and
> initializes
> the consumers lists
>     consumers.append(int(ind))
>     c_counter.append(0)
>     consumers[int(ind)] =
> stackless.tasklet(consumer)(ind)
> 
>
#----------------------------------------------------
> 
> q_size = 20                 # Defines the queue size
> queue = ["a"] * 0           # Defines the queue
> start
> ch_p = stackless.channel()  # Producer channel
> ch_c = stackless.channel()  # Consumer channel
> 
> producers = []              # List to reference the
> producers
> consumers = []              # List to reference the
> consumers
> p_counter = []              # Counter to hold how
> much
> units each producer inserted in queue
> c_counter = []              # Counter to hold how
> much
> units each consumer removed from queue
> 
> num_prod = 1                # Number of starting
> producers
> num_cons = 3                # Number of starting
> consumers
> 
> for p in range(num_prod):
>     launch_p(repr(p))
> 
> for c in range(num_cons):
>     launch_c(repr(c))
> 
> try:
>     stackless.run()
> 
> # Handle the keyboard interruption and prints the
> production report
> 
> except KeyboardInterrupt:
>     print ""
>     print "** Detected ctrl-c in the console"
>     exit
> 
> print
> for p in range(0,len(producers)):
>     print "Producer", p , "produced: ", p_counter[p]
> 
> for c in range(0,len(consumers)):
>     print "Consumer", c , "consumed: ", c_counter[c]
>         
> print "Left in queue: ", len(queue)
> print
> 
> 
> ------------------ Code ------------------
> 
> 
> --- Ásgeir Bjarni Ingvarsson <istari at hlekkir.com>
> escreveu:
> 
> > Hi Carlos.
> > 
> > First of all welcome to the fun world of Stackless
> > Python.
> > The first thing that you need to realize is that
> > this is not
> > actual multithreading, but sometimes the same
> > problems may
> > pop up (i.e. deadlocks). This is also why you can
> > not use
> > time.sleep() since it will stall the entire
> > interpreter.
> > You also have to remember that, if you call
> receive
> > or send
> > on a channel, it will block the calling tasklet.
> > 
> > I played around with your code and came up with
> the
> > following
> > code, I have added comments to explain why I have
> > chosen a
> > particular path.
> 
=== message truncated ===



		
_______________________________________________________ 
Novo Yahoo! Messenger com voz: Instale agora e faça ligações de graça. 
http://br.messenger.yahoo.com/

_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless



More information about the Stackless mailing list