现在,Windows上的(Python 3)任务队列的最佳选择是什么,Celery 4已经放弃了对Windows的支持? [英] What is the best option for a (Python 3) task queue on Windows now that Celery 4 has dropped Windows support?

查看:866
本文介绍了现在,Windows上的(Python 3)任务队列的最佳选择是什么,Celery 4已经放弃了对Windows的支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在Windows下的IIS下运行一个Flask站点,并使用Celery进行进程外任务。 Celery在Windows下给了我们一些问题,但现在我们满意的运行3.1.12版本,使用RabbitMQ / AMQP作为后端,在Windows下工作。



新版本的芹菜(4)删除了对Windows的支持,所以我正在寻找一个可行的选择。 R $似乎是一个非常不错的任务队列,但它也不支持Windows(页面底部)



我已经看到了一些看似不太受欢迎的任务队列,例如:



但是目前还不清楚这些支持Windows和Flask。
我想知道是否有人在Windows下运行Python任务队列的经验。也许是我提到的其中一个,或者是一个替代方案。



运行Linux机器不是我们的选择,因为我们没有管理Linux的经验,在Windows上运行Flaue with Huey没有任何问题,只能用于开发。

和测试。为了生产,我在Linux服务器上使用Flask / Huey。在Redis的后端,Flask 0.12和Huey 1.2.0。

我使用工厂模式为Flask应用程序创建一个专门的减少版本Huey任务的具体使用。这个版本不加载蓝图或配置Flask-Admin,因为这些在Huey任务中不需要。


$ b

__ init__.py的代码示例在应用程序文件夹中 App 是一个从 Flask 扩展的类:

<$ p $如果设置_覆盖:
app.config($ app_config)= code> def create_app(settings_override =无):

app = App('app')

.from_object(settings_override)
else:
app.config.from_object(os.environ ['APP_SETTINGS'])
$ b $ from .ext import configure_extensions
configure_extensions(app ,admin,load_modules = True)

#REST
import rest.api_v1
app.register_blueprint(api_v1_bp,url_prefix ='/ api / v1')

$ ...更多

$ b def create_huey_app():
app = App('huey app')

app.config .from_object(os.environ ['APP_SETTINGS'])
$ b $ from .e import import configure_extensions
configure_extensions(app,admin = None,load_modules = False)

return app

configure_extensions Quokka CMS 。检查应用程序 __ init __。py 及其扩展模块,看看这是如何实现的。还要注意这个项目是如何创建一个特定的应用程序( create_celery_app )用于Celery任务队列的。



tasks.py 。注意使用和app.app_context():来创建Flask上下文。现在我的函数可以使用Flask-Mail,Flask-SqlAlchemy(db,models)等扩展。

$ $ $ $ $ c $ @huey .task()
def generate_transaction_documents_and_email(transaction_id):
app = create_huey_app()
with app.app_context():
reports.generate_transaction_documents_and_email(transaction_id)

$ b $ huey.task $ b $ def send_email(subject,recipients,text_body,html_body,attachments = [],cc = []):
app = create_huey_app()
和app.app_context():
emails.send_email(subject,recipients,text_body,html_body,attachments,cc)

$ b @ huey.periodic_task(crontab(minute =' 30))
如果app.config ['CREATESEND_SYNCHRONIZE']:
_list_name = app.config ['CREATESEND_LIST'] def synchronize_mailing_list():
app = create_huey_app()

with app.app_context():
sync_delete_ar_subscribers(_list_name)
sync_add_ar_subs cribers(_list_name)


We run a Flask site under IIS on Windows, and for out-of-process tasks we use Celery. Celery has given us some problems under Windows, but for now we are satisfied running version 3.1.12, using RabbitMQ/AMQP as a back-end, which works under Windows.

The new version of Celery (4) has dropped support for Windows, so I'm looking for a viable alternative.

RQ seems a very nice task queue, but it also does not support Windows (bottom of the page)

I have seen some more, seemingly less popular task queues like:

But it's unclear if these support Windows and Flask. I'm wondering if anyone has experience running a Python task queue under Windows which works. Maybe one of the ones I mentioned, or an alternative.

It's not an option for us to run a Linux machine, because we have no experience administering Linux, and we have a lot of legacy stuff running that requires Windows.

解决方案

I run Flask with Huey on Windows without any issues, admittedly only for development and testing. For production I use Flask/Huey on Linux servers. Both with the Redis back-end, Flask 0.12 and Huey 1.2.0 .

I use the factory pattern to create a specialised "cut down" version of a Flask app for specific use by Huey tasks. This version doesn't load blueprints or configure Flask-Admin as these aren't required in the Huey tasks.

Example code of __init__.py in app folder. App is a class extending from Flask:

def create_app(settings_override=None):

    app = App('app')

    if settings_override:
        app.config.from_object(settings_override)
    else:
        app.config.from_object(os.environ['APP_SETTINGS'])

    from .ext import configure_extensions
    configure_extensions(app, admin, load_modules=True)

    # REST
    import rest.api_v1
    app.register_blueprint(api_v1_bp, url_prefix='/api/v1')

    #  ... and more suff


def create_huey_app():
    app = App('huey app')

    app.config.from_object(os.environ['APP_SETTINGS'])

    from .ext import configure_extensions
    configure_extensions(app, admin=None, load_modules=False)

    return app

The idea of configure_extensions is taken from Quokka CMS. Examine its app __init__.py and its extensions module to see how this is implemented. Note also how this project too creates a specific app (create_celery_app) for use with the Celery task queue.

Example of tasks.py. Note the use of with app.app_context(): to create the Flask context. Now my functions have access to the extensions such as Flask-Mail, Flask-SqlAlchemy (db, models) etc.

@huey.task()
def generate_transaction_documents_and_email(transaction_id):
    app = create_huey_app()
    with app.app_context():
        reports.generate_transaction_documents_and_email(transaction_id)


@huey.task()
def send_email(subject, recipients, text_body, html_body, attachments=[], cc=[]):
    app = create_huey_app()
    with app.app_context():
        emails.send_email(subject, recipients, text_body, html_body, attachments, cc)


@huey.periodic_task(crontab(minute='30'))
def synchronize_mailing_list():
    app = create_huey_app()
    if app.config['CREATESEND_SYNCHRONIZE']:
        _list_name = app.config['CREATESEND_LIST']
        with app.app_context():
            sync_delete_ar_subscribers(_list_name)
            sync_add_ar_subscribers(_list_name)

这篇关于现在,Windows上的(Python 3)任务队列的最佳选择是什么,Celery 4已经放弃了对Windows的支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆