0

I'd like to schedule a method using the schedule module but it doesn't work.

The error message is as follows:

Warning: Missing Argument 'self'.

Here is my code:

def p_b(self):
    do
    print('hello')
    do
    do

schedule.every().minute.do(p_b)

while True:
    schedule.run_pending()  # error comes out. 
    time.sleep(1)
Isma
  • 14,604
  • 5
  • 37
  • 51

1 Answers1

1

As the error indicates, you added "self" to the method. Self represent an instance of a class that is passed automatically when you make a call, see here fore more info.

So try to remove the self parameter and it should just work:

import schedule
import time

def p_b():
    print('hello')

schedule.every().minute.do(p_b)

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

Edit

If you do have that method inside a class and you have the scheduler outside, you need to call it from an instance of the class e.g.:

import schedule
import time

class SomeClass:
    def p_b(self):
        print('hello')

if __name__=='__main__':
    some_class = SomeClass()

    schedule.every().minute.do(some_class.p_b)

    while True:
        schedule.run_pending()
        time.sleep(1)
Isma
  • 14,604
  • 5
  • 37
  • 51
  • Thank you. But def p_b(self): is a function in certain class so 'self' must need for whole code. any way to use 'schedule' for a function with self in class? – user12908228 Feb 16 '20 at 18:01
  • See my edit, I added an example with a class as well. – Isma Feb 16 '20 at 18:17