Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: can't compare datetime.datetime to NoneType #125

Closed
adtrombley opened this issue Jan 12, 2017 · 12 comments
Closed

TypeError: can't compare datetime.datetime to NoneType #125

adtrombley opened this issue Jan 12, 2017 · 12 comments
Labels
Milestone

Comments

@adtrombley
Copy link

I ran your example, and I got the following error -->

schedule.run_pending()
Traceback (most recent call last):
File "", line 1, in
File "//anaconda/lib/python2.7/site-packages/schedule/init.py", line 462, in run_pending
default_scheduler.run_pending()
File "//anaconda/lib/python2.7/site-packages/schedule/init.py", line 74, in run_pending
for job in sorted(runnable_jobs):
File "//anaconda/lib/python2.7/site-packages/schedule/init.py", line 73, in
runnable_jobs = (job for job in self.jobs if job.should_run)
File "//anaconda/lib/python2.7/site-packages/schedule/init.py", line 368, in should_run
return datetime.datetime.now() >= self.next_run
TypeError: can't compare datetime.datetime to NoneType

@dbader
Copy link
Owner

dbader commented Jan 13, 2017

@adtrombley Can you give some more info on what lines of code came before the run_pending() call? Thanks!

@adtrombley
Copy link
Author

adtrombley commented Jan 13, 2017 via email

@dbader
Copy link
Owner

dbader commented Jan 13, 2017

@adtrombley Mmh... This should work fine on 2.7 and 3.X. I'll need some more context to be able to help you out though :)

@KennyRIM
Copy link

I've also encountered this error.
Looks like, an interrupted job definition chain breaks the module in current session, e.g.:

import schedule
def job():
    print("I'm working...")

schedule.every(10).seconds   #AttributeError: 'NoneType' object has no attribute 'args'

schedule.every(10).seconds.do(job)
schedule.run_pending()  #TypeError: can't compare datetime.datetime to NoneType
# and at this point it's broken until the shell is restarted

The issue would mainly hit those who just wants to play with the module in a python shell.

@jgerardsimcock
Copy link

jgerardsimcock commented Jan 18, 2017

I'm also getting this error using an ipython notebook. I am following the example. It does work, however, if you restart the kernel/shell

@dbader
Copy link
Owner

dbader commented Jan 27, 2017

Thanks @KennyRIM, will look into this. I'm not sure when I'll be able to get to it, so if anyone want's to get started with a PR fix I'd appreciate it.

@dbader dbader added the bug label Jan 27, 2017
@dbader dbader added this to the 0.5.0 milestone Jan 27, 2017
@matuusu
Copy link

matuusu commented Jun 4, 2017

The issue would mainly hit those who just wants to play with the module in a python shell.

IPython runs repr() on job object created in Scheduler.every(), which accesses self.job_func.args (where self.job_func is None at this time) BEFORE it is set in Job.do().

repr() on Job can be called only AFTER Job.do() initializes the Job instance.

the only way I was able to reproduce op's traceback is by incorrect use of schedule.every()

import schedule

schedule.every()
schedule.every(10).seconds.do(lambda:print('lambda'))   # not needed here
schedule.run_pending()

traceback:

Traceback (most recent call last):
  File "R:\test.py", line 5, in <module>
    schedule.run_pending()
  File "c:\Users\matus\Documents\_bin\Miniconda3\lib\site-packages\schedule\__init__.py", line 462, in run_pending
    default_scheduler.run_pending()
  File "c:\Users\matus\Documents\_bin\Miniconda3\lib\site-packages\schedule\__init__.py", line 74, in run_pending
    for job in sorted(runnable_jobs):
  File "c:\Users\matus\Documents\_bin\Miniconda3\lib\site-packages\schedule\__init__.py", line 73, in <genexpr>
    runnable_jobs = (job for job in self.jobs if job.should_run)
  File "c:\Users\matus\Documents\_bin\Miniconda3\lib\site-packages\schedule\__init__.py", line 368, in should_run
    return datetime.datetime.now() >= self.next_run
TypeError: '>=' not supported between instances of 'datetime.datetime' and 'NoneType'

quick fix would be to check for job initialization before run:

class CancelJob(object):
...
    def run_pending(self):
        ## add this line
        [self.jobs.pop(index) for index, job in enumerate(self.jobs) if job.job_func is None]
        runnable_jobs = (job for job in self.jobs if job.should_run)
        for job in sorted(runnable_jobs):
            self._run_job(job)
...

and maybe do the same in run_all()

fix proposal:
matuusu@4b1b15f

@AstraLuma
Copy link

I think I'm getting something similar:

Traceback (most recent call last):
  File "/home/jbliss/miniconda3/envs/xonsh/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/home/jbliss/.xonshrc", line 32, in run
    time.sleep(min(schedule.idle_seconds(), nextsched))
  File "/home/jbliss/miniconda3/envs/xonsh/lib/python3.6/site-packages/schedule/__init__.py", line 487, in idle_seconds
    return default_scheduler.idle_seconds
  File "/home/jbliss/miniconda3/envs/xonsh/lib/python3.6/site-packages/schedule/__init__.py", line 150, in idle_seconds
    return (self.next_run - datetime.datetime.now()).total_seconds()
TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.datetime'

I assume it's from trying to run the scheduler without having any jobs configured.

@dbader
Copy link
Owner

dbader commented Jul 25, 2017

Will be fixed in the next release now that #143 is merged.

@bardiazamanian
Copy link

bardiazamanian commented Jul 25, 2017

Hi @dbader
Could you tag your update?

@SijmenHuizenga
Copy link
Collaborator

Has been released as part of 0.5.0 on nov 16, 2017

@Ben-CN
Copy link

Ben-CN commented Jun 13, 2023

Hi @dbader ,

I had the below error for Schedule 1.1.0 which seems to be the same bug but not sure.

Fatal Python error: none_dealloc: deallocating None: bug likely caused by a refcount error in a C extension
Python runtime state: initialized
Current thread 0x00007f019244c740 (most recent call first):
File "/usr/local/lib/python3.11/site-packages/schedule/init.py", line 661 in run
File "/usr/local/lib/python3.11/site-packages/schedule/init.py", line 172 in _run_job
File "/usr/local/lib/python3.11/site-packages/schedule/init.py", line 100 in run_pending
File "/usr/local/lib/python3.11/site-packages/schedule/init.py", line 780 in run_pending
File "/usr/app/src/./app.py", line 363 in
Extension modules: charset_normalizer.md, _mysql_connector, yaml._yaml (total: 3)

I can't share the full code but its essentially scheduling two different function which each run some API's calls (REST/SOAP) and store the results to MySQL. One runs every minute and one runs every day.

Looking a the logs, the scheduler for the every minute tasks kept running about 30 times before it stopped suddenly. The fatal error is the only clue I was able to find.

schedule.every(1).minutes.do(function1)
schedule.every().day.at("03:30").do(function2)

schedule.run_all()

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

I am updating to schedule 1.2 and hopefully it resolved the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants