Reputation: 1943
First, my code is here:
import schedule # see https://github.com/dbader/schedule
import crawler
def job():
print("Start my scheduled job...")
cw.run()
if __name__ == "__main__":
cw = crawler.crawler()
print("Initial crawling...")
cw.run()
schedule.every(10).seconds.do(job)
while True:
schedule.run_pending()
for title, link in zip(cw.titles, cw.links):
print("%s[%s]" % (title, link))
In the while
loop, I want to execute the for
loop only after the scheduled job finish.
But, that for
loop is running infinitely.
I know why. But I don't know how to fix it.
Can anyone help me?
Upvotes: 2
Views: 1678
Reputation: 520
How about this one?
def job():
print("Start my scheduled job...")
cw.run()
for title, link in zip(cw.titles, cw.links):
print("%s[%s]" % (title, link))
if __name__ == "__main__":
cw = crawler.crawler()
print("Initial crawling...")
cw.run()
schedule.every(10).seconds.do(job)
while True:
schedule.run_pending()
I just moved the for
loop into job()
.
Upvotes: 2
Reputation: 149
I'm sure there is a better way, but what about just putting the for
loop in a function and then passing schedule.run_pending()
into it as a dummy argument. Like this:
def foo(dummy):
for title, link in zip(cw.titles, cw.links):
print("%s[%s]" % (title, link))
while True:
foo(schedule.run_pending())
I haven't tested this code, but I think it should work. Good luck.
Upvotes: 1