我正在为一个网站开发 python 代码,该代码除其他外,还创建一个 excel 工作表,然后将其转换为 json 文件。我需要这段代码持续运行,除非它被网站管理员杀死。
为此,我使用APscheduler。
代码在没有 APscheduler 的情况下可以完美运行,但是当我尝试添加代码的其余部分时,会发生以下两种情况之一: 1)它会永远运行,尽管使用“ctrl+C”也不会停止,我需要使用任务管理器停止它,或者2)它只运行一次,然后就停止了
不停歇的代码:
from apscheduler.scheduler import Scheduler
import logging
import time
logging.basicConfig()
sched = Scheduler()
sched.start()
(...)
code to make excel sheet and json file
(...)
@sched.interval_schedule(seconds = 15)
def job():
excelapi_final()
while True:
time.sleep(10)
sched.shutdown(wait=False)
一次后停止运行的代码:
from apscheduler.scheduler import Scheduler
import logging
import time
logging.basicConfig()
sched = Scheduler()
(...)
#create excel sheet and json file
(...)
@sched.interval_schedule(seconds = 15)
def job():
excelapi_final()
sched.start()
while True:
time.sleep(10)
sched.shutdown(wait=False)
我从其他问题、一些教程和文档中了解到
sched.shutdown
应该允许通过 ctrl+C 杀死代码 - 但这不起作用。有任何想法吗?预先感谢!
您可以使用独立模式:
sched = Scheduler(standalone=True)
然后像这样启动调度程序:
try:
sched.start()
except (KeyboardInterrupt):
logger.debug('Got SIGTERM! Terminating...')
您更正后的代码应如下所示:
from apscheduler.scheduler import Scheduler
import logging
import time
logging.basicConfig()
sched = Scheduler(standalone=True)
(...)
code to make excel sheet and json file
(...)
@sched.interval_schedule(seconds = 15)
def job():
excelapi_final()
try:
sched.start()
except (KeyboardInterrupt):
logger.debug('Got SIGTERM! Terminating...')
这样当按下 Ctrl-C 时程序就会停止
你可以优雅地关闭它:
import signal
from apscheduler.scheduler import Scheduler
import logging
import time
logging.basicConfig()
sched = Scheduler()
(...)
#create excel sheet and json file
(...)
@sched.interval_schedule(seconds = 15)
def job():
excelapi_final()
sched.start()
def gracefully_exit(signum, frame):
print('Stopping...')
sched.shutdown()
signal.signal(signal.SIGINT, gracefully_exit)
signal.signal(signal.SIGTERM, gracefully_exit)
我尝试了第一个答案,我的代码排列如下:
class APSchedulerBase:
def __init__(self, scheduler: Any = None, add_listener: bool = True):
if scheduler is None:
self.sched = BlockingScheduler()
if add_listener:
self.sched.add_listener(self.ap_listener, EVENT_JOB_ERROR | EVENT_JOB_EXECUTED)
else:
self.sched = scheduler
def ap_listener(self, event):
if event.exception == KeyboardInterrupt or event.exception == SystemExit:
self.on_kill()
if event.exception:
logger.critical(str(traceback.format_exc()))
else:
pass
def on_kill(self, sig=None, frame=None):
logger.warning("Scheduler Exit.")
self.sched.shutdown(wait=False)
def register_task(
self, func: Callable, trigger: str,
**kwargs
):
@sched_job(
self.sched,
logger,
trigger=trigger,
**kwargs
)
def wrap():
func()
wrap()
def start(self):
try:
logger.warning("Scheduler Start.")
self.sched.start()
except (KeyboardInterrupt, SystemExit):
self.on_kill()
但是当进程等待 cron 作业执行时,我无法使用 ctrl+c 杀死该进程。好像kill信号只有在job即将执行的时候才能传达?