Python APScheduler无法完全关闭

时间:2019-03-02 00:42:45

标签: python apscheduler

我正在尝试使用apscheduler收集图像数小时,然后关闭。以下代码运行,然后关闭,但在该过程中引发异常。我将此异常表示我没有正确关闭它。

import os
from datetime import datetime
from datetime import timedelta
from apscheduler.schedulers.blocking import BlockingScheduler

def snap_images():
    print('some message')

def end_sequence():
    scheduler.remove_job('snap_images')
    scheduler.shutdown(wait=True)

if __name__ == '__main__':

    end_time = datetime.now() + timedelta(seconds=5)

    # Setup and start the scheduler
    scheduler = BlockingScheduler()
    scheduler.add_job(snap_images, 'interval', seconds=.5, id='snap_images')
    scheduler.add_job(end_sequence,'date',run_date=end_time)

    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    try:
        scheduler.start()
    except (KeyboardInterrupt, SystemExit):
        pass

    print('scheduler is shut down')

以下是输出:

Press Ctrl+Break to exit 
some message 
some message 
some message 
some message 
some message 
some message 
some message 
some message 
Job "end_sequence (trigger: date[2019-03-04 09:12:42 PST], next run at: 2019-03-04 09:12:42 PST)" raised an exception Traceback (most recent call last):   
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\executors\base.py", line 125, in run_job
            retval = job.func(*job.args, **job.kwargs)   
File "exitAPSched.py", line 12, in end_sequence
            scheduler.shutdown(wait=True)   
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\schedulers\blocking.py", line 22, in shutdown
            super(BlockingScheduler, self).shutdown(wait)   
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\schedulers\base.py", line 187, in shutdown
            executor.shutdown(wait)   
File "C:\ProgramData\Anaconda3\lib\site-packages\apscheduler\executors\pool.py", line 26, in shutdown
            self._pool.shutdown(wait)   File "C:\ProgramData\Anaconda3\lib\concurrent\futures\thread.py", line 152, in shutdown
            t.join()   
File "C:\ProgramData\Anaconda3\lib\threading.py", line 1053, in join
            raise RuntimeError("cannot join current thread") RuntimeError: cannot join current thread 
scheduler is shut down

0 个答案:

没有答案