无法使用apscheduler在后台执行任务

时间:2019-06-25 22:05:39

标签: python-2.7 apscheduler

我以前使用过Blockingscheduler,但是使用Backgroundscheduler却遇到了问题。

需要在返回值后运行调度程序任务,但是从未执行调度任务。

interface Props {
    testQueues: any;
}

interface State {
    queues: any
}

@observer
class DeadletterLogic extends Component<Props, State> {

    constructor(props: any) {
        super(props);
        this.state = {
            queues: []
        };
        this.getQueues();
    }

    // here is where the business logic will be done
    getQueues() {
        // in order to gets a list of queues here, a fetch call must be made in the service layer to the MessageQueueAPI
    }

    componentDidMount() {
        // when passing from a parent, I'm not sure if this needs to live here or if it can live in getQueues to update when the fetch is finished.
        this.setState({ queues: this.props.testQueues });
    }

    render() {
        return (
            <div>
                {this.props.testQueues}
            </div>
        );
    }
}

我只得到“ hello”的输出,并且代码正在退出。预期的输出是“ hello”和“ text”,应在20秒后执行一次。请让我知道我搞砸了!

1 个答案:

答案 0 :(得分:0)

您可能会发现this FAQ entry很有启发性。

总而言之,除非非守护线程处于活动状态,否则Python脚本一旦到达末尾将退出。默认情况下,调度程序线程是守护程序。

此外,在函数中创建新的调度程序并且不将实例保存在全局变量中是一种不好的做法,该全局变量可用于调度其他作业或关闭调度程序。您的代码现在的工作方式是它将继续创建新的调度程序,而不会关闭以前的调度程序。

相关问题