我的意图是在凌晨2点到早上6点之间的随机时间,每周随机发布一次列表上的随机信息。我正在使用APScheduler:
sched.add_cron_job(postTweet(messages[random.randint(0, len(messages))]), day_of_week="0-6/6", hour='2-6/3')
我得到了错误:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/apscheduler/scheduler.py", line 379, in add_cron_job
sched.add_cron_job(postTweet(messages[random.randint(0, len(messages))]), day_of_week="0-6/6", hour='2-6/3')
options.pop('coalesce', self.coalesce), **options)
File "/app/.heroku/python/lib/python2.7/site-packages/apscheduler/job.py", line 47, in __init__
raise TypeError('func must be callable')
TypeError: func must be callable
return self.add_job(trigger, func, args, kwargs, **options)
我不知道这个错误意味着什么,更不用说如何修正它了。任何关于要研究的东西的启示都会受到极大的感谢。
编辑:
postTweet的代码:
def postTweet(message):
log = open('log', 'a')
log.write("\nMessage being tweeted: %s \n" % message)
print "Message being tweeted: %s" % message
twitter.statuses.update(status=message)
log.close()
发布于 2013-11-05 23:39:13
您的func
不是一个函数,就像它说的那样。您正在调用PostTweet
并将结果(可能是字符串或None
,但肯定不是函数)传递给add_cron_job
。那条狗不会打猎的,主教大人。把一个lambda:
放在它的前面:
sched.add_cron_job(lambda: postTweet(messages[random.randint(0, len(messages))]),
day_of_week="0-6/6", hour='2-6/3')
这将创建一个可以在以后调用的函数,而不是在添加作业之前执行它。
发布于 2018-03-11 16:17:07
我知道我迟到了,但我想帮助像我这样的人。
您可以使用args作为
scheduler.add_job(postTweet, day_of_week="0-6/6", hour='2-6/3' args='Your Payload as dict')
https://stackoverflow.com/questions/19801242
复制相似问题