Skip to content

Instantly share code, notes, and snippets.

@xiantail
Last active December 28, 2015 09:58
Show Gist options
  • Save xiantail/7f935030cd0fd7fb946c to your computer and use it in GitHub Desktop.
Save xiantail/7f935030cd0fd7fb946c to your computer and use it in GitHub Desktop.
Introducing Python / Chap.11 Exercise 11.4
import redis
import random
from time import sleep
conn = redis.Redis()
chocolates = ['sweet', 'bitter', 'milk', 'white', 'green tea']
while True:
sleep(random.random())
chocolate = random.choice(chocolates)
conn.rpush('chocolates', chocolate)
import redis
from time import sleep
from datetime import datetime
import random
conn = redis.Redis()
print('Lucy is ready...')
timeout = 20
while True:
sleep(0.5)
msg = conn.blpop('chocolates', timeout)
remaining = conn.llen('chocolates')
if msg:
chocolate = msg[1]
print("Lucy got a", chocolate, "at", datetime.now(), ". Still remaining", remaining, "pieces")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment