qwerty
qwerty

Reputation: 195

error when calling python scheduling function

I try to call the get_registers function on a schedule. I get an error TypeError: the first argument must be callable.

import requests, time, datetime, json, schedule 


class Registration:

    url = 'http://127.0.0.1:8000/api/reg/'

    def get_registers(self):
        now = datetime.datetime.now()
        date = now.strftime("%d-%m-%Y")
        full_page = requests.get(self.url, auth=("admin","admin"))
        pars=json.loads(full_page.content.decode('utf-8'))
        a=sorted(pars, key=lambda pars: pars['time_visit'])
        count=0
        for i in a:
            if i['date_visit']==date:
                count +=1
                print(i["number_car"], i['time_visit'])


reg = Registration()
schedule.every(1).minutes.do(reg.get_registers())

while True:
    schedule.run_pending()
    time.sleep(1)

I just can’t understand what I missed.

Upvotes: 1

Views: 324

Answers (1)

Konstantin
Konstantin

Reputation: 25379

schedule.every(1).minutes.do(reg.get_registers())

Should be

schedule.every(1).minutes.do(lambda: reg.get_registers)

The difference is the following. In the first case, you schedule to execute reg.get_registers() result (which is None since you do not return anything from that method) every minute. In the second case, you schedule to execute reg.get_registers itself every minute.

Upvotes: 3

Related Questions