0

所以我试图弄清楚为什么选择不适用于调度程序。目前它不会每次都选择新的说法。调度程序运行良好,并且我在其他情况下成功使用了选择。

那么我在这里做错了什么?如果该信息有用,我也会通过解释器说“import [project name]”来运行它。

谢谢!

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()
4

1 回答 1

1

您只choice(cat_sayings)在模块的顶层调用一次,并且再也不会调用。所以,它会随机选择一个,永远不会选择一个新的。

要解决此问题,只需将代码移动到函数中:

def cat_job(sayings):
    print choice(sayings)

# ...

s.add_cron_job(cat_job, args=[cat_sayings], second='*/30')
于 2013-11-12T19:06:15.827 回答