Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将对象作为函数的参数传递给作业调度程序_Python 3.x_Apscheduler - Fatal编程技术网

Python 3.x 将对象作为函数的参数传递给作业调度程序

Python 3.x 将对象作为函数的参数传递给作业调度程序,python-3.x,apscheduler,Python 3.x,Apscheduler,我尝试通过apscheduler将对象作为参数传递给作业函数。 这很好,但在我的例子中,我想更改它的一个值,并在触发作业时使用更新的值。 这是我的示例代码 import time import sqlalchemy from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore class MyCl

我尝试通过apscheduler将对象作为参数传递给作业函数。 这很好,但在我的例子中,我想更改它的一个值,并在触发作业时使用更新的值。 这是我的示例代码

import time
import sqlalchemy
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore


class MyClass:
    def __init__(self, *args, **kwargs):
        self.state = kwargs.get('state', "")


jobstores = {
    'default': SQLAlchemyJobStore(url='sqlite:///sched.db', tablename='apscheduler_jobs')
}

scheduler = BackgroundScheduler()
scheduler.configure(timezone='Europe/Paris')
scheduler.add_jobstore(jobstores['default'], 'default')


def myFunction(_internals):
    print("- in job")
    print(_internals.__dict__)
    print(".")


if __name__ == "__main__":
    scheduler.start()
    myInstance = MyClass(state="off")
    print(myInstance.__dict__)
    j1 = scheduler.add_job(myFunction, trigger='cron', args=[myInstance],  second='*/10', max_instances=10, jobstore='default', srv_id="blablabla-x6548710")
    try:
        # This is here to simulate application activity (which keeps the main thread alive).
        while True:
            time.sleep(2)
            myInstance.__setattr__("state", "running")
            print(myInstance.__dict__)
    except (KeyboardInterrupt, SystemExit):
        print('exit')
        scheduler.shutdown()
以下是我的期望:

{'state': 'running'}
{'state': 'running'}
{'state': 'running'}
{'state': 'running'}
{'state': 'running'}
in job
{'state': 'running'}
.
{'state': 'running'}
但我有:

{'state': 'running'}
{'state': 'running'}
{'state': 'running'}
{'state': 'running'}
{'state': 'running'}
in job
{'state': 'off'}
.
{'state': 'running'}
有没有办法在while循环和触发此循环时的作业中具有相同的值


谢谢你们,男孩和女孩们,原来我的工作是一个完全不同的对象。 因此,它不受循环中所做任何更改的影响。 我使用了另一种策略: 使用memcached在循环和计划作业之间进行通信。 仍然接受任何其他建议