Question

So I'm trying to figure out why choice isn't working with a scheduler. At the moment it doesn't pick a new saying each time. The scheduler runs fine, and choice I've used successfully in in other contexts.

So what am I doing wrong here? Also I run this through the interpreter by saying "import [project name]" if that info is useful.

Thanks!

from apscheduler.scheduler import Scheduler
from random import choice

#change this to a text file
cat_sayings = [
"I can haz?",
"I pooped in your shoe.",
"I ate the fish.",
"I want out.",
"Food? What...? Food?",
"When are you coming home? There's food that needs eating!",
"Lulz, I am sleeping in your laundry.",
"I didn't do it. Nope."]

sayings = choice(cat_sayings)

def cat_job(sayings):

    print sayings


s = Scheduler()
s.add_cron_job(cat_job, args=[sayings], second='*/30')
s.start()
Was it helpful?

Solution

You're only calling choice(cat_sayings) once, at the top level of your module, and never again. So, it's going to pick one random choice and never pick a new one.

To fix this, just move the code into the function:

def cat_job(sayings):
    print choice(sayings)

# ...

s.add_cron_job(cat_job, args=[cat_sayings], second='*/30')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top