APScheduler查找对象错误

时间:2017-04-06 03:15:37

标签: apscheduler

APScheduler(3.3.1)py2.7

我使用此代码来完成我的工作当我使用内存作为工作存储时它可以正常工作但是我有太多工作而且我的服务器内存是限制所以我将SQLAlchemyJobStore更改为工作存储但我收到了查找错误。如何解决它。

代码:

def script(indicator, strategy_name, real_time=False):
    # Solve No handlers could be found for logger “apscheduler.scheduler
    import logging
    logging.basicConfig(level=logging.ERROR,
                        format='%(name)-12s %(asctime)s %(levelname)-8s %(message)s',
                        datefmt='%a, %d %b %Y %H:%M:%S')
    try:
        job_defaults = {
            'coalesce': False,
            'max_instances': 1,
            "misfire_grace_time": config.real_time_script_interval + 5,
        }
        executors = {
            'default': ThreadPoolExecutor(60),
        }
        jobstores = {
            "default": SQLAlchemyJobStore(url='sqlite:///jobs.sqlite')
        }

        scheduler = BlockingScheduler(daemonic=True, jobstores=jobstores, job_defaults=job_defaults,
                                      executors=executors)
        module = __import__("%s.%s" % (indicator, strategy_name), fromlist=[strategy_name])
        if real_time:
            for st in module.strategy:
                scheduler.add_job(st.run, "interval", seconds=config.real_time_script_interval)
        else:
            for st in module.strategy:
                # 计算最近的下一个准点时间
                start_time = _recent_time(st.run_period)
                scheduler.add_job(st.run, "interval", **start_time)
        scheduler.start()
    except Exception as e:
        logger.get("run-log").error(error_msg())

错误:

apscheduler.jobstores.default Thu, 06 Apr 2017 10:50:32 ERROR    Unable to restore job "d100a4b24e2d49c3ad51305fd846e5f5" -- removing it
Traceback (most recent call last):
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 135, in _get_jobs
    jobs.append(self._reconstitute_job(row.job_state))
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 122, in _reconstitute_job
    job.__setstate__(job_state)
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/job.py", line 260, in __setstate__
    self.func = ref_to_obj(self.func_ref)
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/util.py", line 277, in ref_to_obj
    raise LookupError('Error resolving reference %s: error looking up object' % ref)
LookupError: Error resolving reference base.strategy:Strategy.run: error looking up object
apscheduler.jobstores.default Thu, 06 Apr 2017 10:50:32 ERROR    Unable to restore job "2602167cd3c745c2b0764a2b63da1a3a" -- removing it
Traceback (most recent call last):
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 135, in _get_jobs
    jobs.append(self._reconstitute_job(row.job_state))
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 122, in _reconstitute_job
    job.__setstate__(job_state)
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/job.py", line 260, in __setstate__
    self.func = ref_to_obj(self.func_ref)
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/util.py", line 277, in ref_to_obj
    raise LookupError('Error resolving reference %s: error looking up object' % ref)
LookupError: Error resolving reference base.strategy:Strategy.run: error looking up object
apscheduler.jobstores.default Thu, 06 Apr 2017 10:50:32 ERROR    Unable to restore job "3eb917670e7642b8848a165268df8913" -- removing it
Traceback (most recent call last):
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 135, in _get_jobs
    jobs.append(self._reconstitute_job(row.job_state))
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/jobstores/sqlalchemy.py", line 122, in _reconstitute_job
    job.__setstate__(job_state)
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/job.py", line 260, in __setstate__
    self.func = ref_to_obj(self.func_ref)
  File "/Users/wyx/bitcoin_workspace/fibo-strategy/.env/lib/python2.7/site-packages/apscheduler/util.py", line 277, in ref_to_obj
    raise LookupError('Error resolving reference %s: error looking up object' % ref)
LookupError: Error resolving reference base.strategy:Strategy.run: error looking up object
AlexGrönholm在这里的问题

补充说明,因为评论很难说

在base / strategy_util.py中:

base_strategy 继承 base / strategy.py 中的BaseStrategy类的一些类。 BaseStrategy有运行方法

def _strategy(base_strategy, minute, ticker_table_format):
    class Strategy(base_strategy):
        run_period = minute

        def _init_params(self):
            self.ticker_table_format = ticker_table_format

    return Strategy()


def _create_strategy(base_strategy, minute_list=ALL_MINUTE):
    strategy_list = []
    for minute in minute_list:
        for ticker_table_format in const.TICKER_TABLE_FORMAT.ALL:
            st = _strategy(base_strategy, minute, ticker_table_format)
            strategy_list.append(st)
    return strategy_list


def ma_strategy(base_strategy):
    return _create_strategy(base_strategy)

在MA / touch_avg.py中:

MA_TOUCH_AVG 继承 BaseStrategy类

from base.strategy import MA_TOUCH_AVG
from base.strategy_util import ma_strategy

strategy = ma_strategy(MA_TOUCH_AVG)

然后我使用click来调用python run_strategy.py run MA touch_avg

之类的策略

在run_strategy.py中:

@cli.command()
@click.argument('indicator')
@click.argument('strategy_name')
def run(indicator, strategy_name):
    """ run indicator strategy_name """
    real_time_strategy_name = ["touch_avg", "limit"]
    util.script(indicator, strategy_name,
                real_time=strategy_name in real_time_strategy_name)

1 个答案:

答案 0 :(得分:2)

您遇到此问题的原因是因为您在函数中动态创建了一个类。 APScheduler将对计划函数的引用存储为module:varname。预期调度程序如何找到您在函数中即时创建的类?