所以我试图弄清楚为什么选择不适用于调度程序。目前它每次都没有发表新的说法。调度程序运行正常,我在其他环境中成功使用的选项。
那我在这里做错了什么?另外,我通过解释器通过说“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()
答案 0 :(得分:1)
您只需在模块的顶层调用choice(cat_sayings)
一次,而不会再次调用def cat_job(sayings):
print choice(sayings)
# ...
s.add_cron_job(cat_job, args=[cat_sayings], second='*/30')
。因此,它将选择一个随机选择,而不是选择一个新选项。
要解决此问题,只需将代码移动到函数中:
{{1}}