从另一个 .py 文件方法停止 apscheduler 作业

Stop apscheduler job from another .py file method

我正在一个函数中创建一个 apscheduler 作业,在 before_first_request() 的 flask 应用程序中调用这个函数。此作业每 30 秒触发一个函数(在我的烧瓶应用程序的另一个 .py 文件中)。基于一个特定的结果(来自另一个 .py 文件),我必须终止这个 apscheduler 作业。

In app.py:
@myapp.before_first_request
def before_first_request():
    ..
    ..
    check_conn()

then in checking.py, I'm creating and starting a Background_scheduler job.
def check_conn():
    with myapp.test_request_context():
        global process_scheduler
        process_scheduler = BackgroundScheduler()
        process_scheduler.add_job(func=send_conn_check, trigger="interval", seconds=10, id='conn_job')
        process_scheduler.start()
        atexit.register(lambda: process_scheduler.shutdown())

this 'send_conn_check' is in sendxx.py:
def send_conn_check():
    ....  # sends message at regular time interval
    ....

When a message is received for first time in backgrnd.py, the below function will do some work and then call 'check_state' method to remove the apscheduler job,
def call_conn_accept(ch, method, properties, body):
    try:
        print('Connection message from abc %r' % body)
        json_str = body.decode('utf-8')
        ...
        if accept == "ACCEPT":
            check_state()  # this function is defined in checking.py

Again in checking.py, 
def check_state():
    global process_scheduler
    process_scheduler.remove_job(job_id='conn_job') ####  At this point I have remove the process_scheduler job. 
    ....
    ....

node1_CONN # 调度器发送消息

node1_CONN

node1_CONN

ERROR:root:An exception has occurred:("name 'process_scheduler' is not defined",) # when process_scheduler in check_state() in checking.py is called to remove the apscheduler job .

来自 abc 的连接消息'{"node1": "ACCEPT"}'

node1_CONN # 一旦收到上述消息,调度程序应该删除作业,但它没有。它再次发送 'node1_CONN'

错误:root:发生异常:("name 'process_scheduler' is not defined",)

来自 abc 的连接消息'{"node1": "ACCEPT"}' ... ...

我通过将逻辑放在 python 中的多处理进程中解决了这个问题,还在这个多进程守护进程中启动了一个线程,每隔 x 秒调用一次 'send' 函数,并在出现条件时取消线程在同一个多进程守护进程的另一个函数中遇到..

from multiprocessing import Process

class processClass:
    def __init__(self):
        print("Starting the multiprocess app...")
        p = Process(target=self.run, args=())
        p.daemon = True                       # Daemonize it
        p.start()                             # Start the execution

  def run(self):
        #with myapp.test_request_context():
        print('listening...')
        send_conn_check()                     # Calling send function thread 
        listen_state()                        # Calling receive function.

def send_conn_check():
    global t                              
    t = threading.Timer(10, send_conn_check)  #Thread
    t.daemon = True
    t.start()
    ...
    ...

def listen():
  try:
    ...
    ...
    if accept == 'ACCEPT':
        t.cancel()                           # Cancel the thread when a condition is met
        check_state()