[Stackless] Starting in stackless...
Ásgeir Bjarni Ingvarsson
istari at hlekkir.com
Wed Mar 22 20:58:14 CET 2006
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.
------
import stackless
import time
q_size = 15
queue = []
ch_p = stackless.channel()
ch_c = stackless.channel()
print ""
# 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()
def printStatus(reporter):
print reporter + "[" + "#" * len(queue) + " " * (15-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():
while True:
if (len(queue) < q_size):
queue.append("a")
printStatus('P')
delay(0.3)
else:
ch_c.send(None)
ch_p.receive()
def consumer():
while True:
if (len(queue) >= 1):
queue.pop()
printStatus('C')
delay(0.9)
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)
prod = stackless.tasklet(producer)()
cons = stackless.tasklet(consumer)()
stackless.run()
------
Best regards,
Asgeir
Carlos Eduardo de Paula wrote:
> Hello,
>
> I just subscribed in the list.. I´m starting up in
> stackless and haven´t done much things with
> multithread so i´m a little confused.
>
> I created a little program to test the
> producer/consumer chain.. but with no success... maybe
> someone could give me a hint about how to make it
> work... or point some documentation about it...
>
> ----------------
>
> import stackless
> import time
>
> q_size = 15
> queue = []
> ch_p = stackless.channel()
> ch_c = stackless.channel()
>
> print ""
>
> def producer():
> while True:
> if (len(queue) < q_size):
> queue.append("a")
> print "P[" + "#" * len(queue) + " " *
> (15-len(queue)) + "]" + "\r",
> time.sleep(0.3)
> stackless.schedule()
> else:
> ch_c.send(None)
> ch_p.receive()
>
>
> def consumer():
> while True:
> if (len(queue) >= 1):
> queue.pop()
> print "C[" + "#" * len(queue) + " " *
> (15-len(queue)) + "]" + "\r",
> time.sleep(0.9)
> stackless.schedule()
> else:
> ch_p.send(None)
> ch_c.receive()
>
> prod = stackless.tasklet(producer)
> cons = stackless.tasklet(consumer)
>
> stackless.run()
>
>
> ----------------
>
>
> Thanks for all,
>
> Carlos
>
>
>
>
>
>
>
>
> _______________________________________________________
> Yahoo! doce lar. Faça do Yahoo! sua homepage.
> http://br.yahoo.com/homepageset.html
>
>
> _______________________________________________
> Stackless mailing list
> Stackless at stackless.com
> http://www.stackless.com/mailman/listinfo/stackless
>
>
> ----------------------------------------
> This message was checked with
> SpamAssassin v.3.10 and ClamWin Antivirus v0.88
>
----------------------------------------
This message was checked with
SpamAssassin v.3.10 and ClamWin Antivirus v0.88
_______________________________________________
Stackless mailing list
Stackless at stackless.com
http://www.stackless.com/mailman/listinfo/stackless
More information about the Stackless
mailing list