2017-06-12 88 views
0

我已阅读官方文档,但我不太确定我了解如何应用他们所说的内容。我也见过this QA,我也使用工厂模式。只是看不到整个画面。如何将Huey连接到Flask应用程序

  1. 连接池,只要其他redis的/休伊设置可根据给定的环境(发展生产)不同。我们如何连接huey以便我们可以配置它类似于Flask应用程序?

  2. 只要我理解从视图中触发任务,我们需要导入任务moudule并调用特定任务(调用传递敏感参数的函数)。我们在哪里实例化,保持huey实例?

  3. 任务应该知道应用程序依赖关系吗?我们是否应该为这件事考虑另一个精简版Flask应用程序?

你能帮助一下吗?

回答

2

下面是我如何连接它。

首先,这里是我的项目文件夹中的内容:

enter image description here

  1. 获取到你的任务使用简装瓶的应用程序。至于有人建议in the post我创建了一个辅助应用工厂:

    # global dependencies 
    db = SQLAlchemy() 
    
    def create_app_huey(config_name): 
    
        app = Flask(__name__) 
    
        # apply configuration 
        app.config.from_object(config[config_name]) 
    
        # init extensions 
        db.init_app(app) 
    
        return app 
    
  2. 创建任务包。这里有两个重要的文件是config.pytasks.pyThis post helped a lot。我们从配置开始。请注意,这是非常简单的方法。

    # config.py (app.tasking.config) 
    
    import os 
    from huey import RedisHuey 
    
    
    settings__development = { 
        'host': 'localhost' 
    } 
    
    settings__testing = { 
        'host': 'localhost' 
    } 
    
    settings__production = { 
        'host': 'production_server' 
    } 
    
    settings = { 
        'development': settings__development, 
        'testing': settings__testing, 
        'production': settings__production, 
        'default': settings__development 
    } 
    
    huey = RedisHuey(**settings[os.getenv('FLASK_ENV') or 'default']) 
    

    然后tasks.py模块看起来就像这样:

    import os 
    from app.tasking.config import huey 
    from app import create_app_huey 
    
    
    app = create_app_huey(config_name=os.getenv('FLASK_ENV') or 'default') 
    
    
    @huey.task() 
    def create_thumbnails(document): 
        pass 
    
  3. 运行消费者。激活你的虚拟环境。然后从CMD(我在Windows上)运行:

    huey_consumer.py app.tasking.config.huey

    app.tasking.config包装模块路径(在我的情况!)和 huey是可用的名称(在配置模块中)huey实例。检查您的huey实例名称。

    Reading this helped

+1

很高兴你成功了!我已经添加了一个github [flask-huey-example](https://github.com/pjcunningham/flask-huey-example)项目,其他人可能会觉得有用。 – pjcunningham

+0

@pjcunningham已经在看它!谢谢! – lexeme

相关问题