2

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?

Yonggoo Noh
  • 1,811
  • 3
  • 22
  • 37

2 Answers2

2

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

yakkisyou
  • 510
  • 7
  • 14
1

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.

Bryan Bugyi
  • 149
  • 5