Apscheduler 运行一次然后抛出 TypeError
Apscheduler runs once then throws TypeError
我试图每小时将某人的 soundcloud 关注者列表添加到数据库中。我有代码可以提取他们的关注者列表并将它们添加到数据库中,但是当我将它与 apscheduler 一起使用时,我 运行 出错了。
错误示例如下:
Traceback (most recent call last):
File "desktop/SoundcloudProject/artistdailyfollowers.py", line 59, in <module>
scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
File "//anaconda/lib/python3.5/site-packages/apscheduler/schedulers/base.py", line 425, in add_job
job = Job(self, **job_kwargs)
File "//anaconda/lib/python3.5/site-packages/apscheduler/job.py", line 44, in __init__
self._modify(id=id or uuid4().hex, **kwargs)
File "//anaconda/lib/python3.5/site-packages/apscheduler/job.py", line 165, in _modify
raise TypeError('func must be a callable or a textual reference to one')
TypeError: func must be a callable or a textual reference to one
代码如下:
import soundcloud
import sqlite3
import datetime
import time
from apscheduler.schedulers.blocking import BlockingScheduler
client = soundcloud.Client(client_id='f3b669e6e4509690939aed943c56dc99')
conn = sqlite3.connect('desktop/SoundcloudProject/RageLogic.db')
c = conn.cursor()
writenow = datetime.datetime.now()
print("If this is printing that means it's running")
print("The time is now: \n" +str(writenow))
page ='https://soundcloud.com/ragelogic'
page_size = 200
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS RageLogicFollowersByDay(day TEXT, list_of_followers TEXT)')
#add todays date and a list of followers to the db
def inserttodaysdata():
global page
full = False
user = client.get('/resolve', url=page)
ufollowing = []
ufirstfollowing = client.get('/users/'+str(user.id)+'/followers', order='id',limit=page_size, linked_partitioning=1)
for user in ufirstfollowing.collection:
ufollowing.append(user.id)
if hasattr(ufirstfollowing, "next_href"):
#print("MANYFOLLOWING")
newlink = ufirstfollowing.next_href
try:
while full == False:
newlist = client.get(newlink)
for user in newlist.collection:
ufollowing.append(user.id)
print(len(ufollowing))
if newlist.next_href == None:
print("full")
full = True
else:
newlink = newlist.next_href
except AttributeError:
None
#print(len(ufollowing))
wtl = []
wtl = repr(ufollowing)
writenow = datetime.datetime.now()
c.execute("INSERT INTO RageLogicFollowersByDay (day, list_of_followers) VALUES (?, ?)",(str(writenow), wtl))
conn.commit()
#create_table()
scheduler = BlockingScheduler()
scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
scheduler.start()
我对这整件事真的很陌生,任何人都可以提供任何帮助都很棒,谢谢!
看到这一行:
scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
应该是
scheduler.add_job(inserttodaysdata, 'interval', hours=1)
您正在调用 inserttodaysdata()
并将其 return 值 传递给 add_job()
。不要那样做。传递函数本身,而不是它的调用结果。
我只添加 time-zone:
就解决了
scheduler = BackgroundScheduler(timezone="Europe/Berlin")
我试图每小时将某人的 soundcloud 关注者列表添加到数据库中。我有代码可以提取他们的关注者列表并将它们添加到数据库中,但是当我将它与 apscheduler 一起使用时,我 运行 出错了。
错误示例如下:
Traceback (most recent call last):
File "desktop/SoundcloudProject/artistdailyfollowers.py", line 59, in <module>
scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
File "//anaconda/lib/python3.5/site-packages/apscheduler/schedulers/base.py", line 425, in add_job
job = Job(self, **job_kwargs)
File "//anaconda/lib/python3.5/site-packages/apscheduler/job.py", line 44, in __init__
self._modify(id=id or uuid4().hex, **kwargs)
File "//anaconda/lib/python3.5/site-packages/apscheduler/job.py", line 165, in _modify
raise TypeError('func must be a callable or a textual reference to one')
TypeError: func must be a callable or a textual reference to one
代码如下:
import soundcloud
import sqlite3
import datetime
import time
from apscheduler.schedulers.blocking import BlockingScheduler
client = soundcloud.Client(client_id='f3b669e6e4509690939aed943c56dc99')
conn = sqlite3.connect('desktop/SoundcloudProject/RageLogic.db')
c = conn.cursor()
writenow = datetime.datetime.now()
print("If this is printing that means it's running")
print("The time is now: \n" +str(writenow))
page ='https://soundcloud.com/ragelogic'
page_size = 200
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS RageLogicFollowersByDay(day TEXT, list_of_followers TEXT)')
#add todays date and a list of followers to the db
def inserttodaysdata():
global page
full = False
user = client.get('/resolve', url=page)
ufollowing = []
ufirstfollowing = client.get('/users/'+str(user.id)+'/followers', order='id',limit=page_size, linked_partitioning=1)
for user in ufirstfollowing.collection:
ufollowing.append(user.id)
if hasattr(ufirstfollowing, "next_href"):
#print("MANYFOLLOWING")
newlink = ufirstfollowing.next_href
try:
while full == False:
newlist = client.get(newlink)
for user in newlist.collection:
ufollowing.append(user.id)
print(len(ufollowing))
if newlist.next_href == None:
print("full")
full = True
else:
newlink = newlist.next_href
except AttributeError:
None
#print(len(ufollowing))
wtl = []
wtl = repr(ufollowing)
writenow = datetime.datetime.now()
c.execute("INSERT INTO RageLogicFollowersByDay (day, list_of_followers) VALUES (?, ?)",(str(writenow), wtl))
conn.commit()
#create_table()
scheduler = BlockingScheduler()
scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
scheduler.start()
我对这整件事真的很陌生,任何人都可以提供任何帮助都很棒,谢谢!
看到这一行:
scheduler.add_job(inserttodaysdata(), 'interval', hours=1)
应该是
scheduler.add_job(inserttodaysdata, 'interval', hours=1)
您正在调用 inserttodaysdata()
并将其 return 值 传递给 add_job()
。不要那样做。传递函数本身,而不是它的调用结果。
我只添加 time-zone:
就解决了scheduler = BackgroundScheduler(timezone="Europe/Berlin")