异步任务用celery是不是个不错的选择?

12 views
Skip to first unread message

Chunlin Zhang

unread,
Oct 23, 2014, 10:57:02 AM10/23/14
to uli...@googlegroups.com
上次在Q群里讨论过异步任务,我刚看到celery看起来不错,不知道limodou有木有试过.

limodou

unread,
Oct 23, 2014, 7:47:10 PM10/23/14
to uli...@googlegroups.com
我没有。我正琢磨着自已写个简单的呢。

On Thu, Oct 23, 2014 at 10:57 PM, Chunlin Zhang <zhangc...@gmail.com> wrote:
上次在Q群里讨论过异步任务,我刚看到celery看起来不错,不知道limodou有木有试过.

--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+un...@googlegroups.com.
To post to this group, send email to uli...@googlegroups.com.
Visit this group at http://groups.google.com/group/uliweb.
To view this discussion on the web visit https://groups.google.com/d/msgid/uliweb/CAG2rzA%3DgnpFuKZJaDfTBSQ%3DFkfDezjQquRKNZ-Ebkc4CDTuOuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou

Chunlin Zhang

unread,
Jun 24, 2015, 2:50:22 AM6/24/15
to uli...@googlegroups.com, lim...@gmail.com
现在这块咋样了呢?

在 2014年10月24日星期五 UTC+8上午7:47:10,limodou写道:
我没有。我正琢磨着自已写个简单的呢。

On Thu, Oct 23, 2014 at 10:57 PM, Chunlin Zhang <zhangc...@gmail.com> wrote:
上次在Q群里讨论过异步任务,我刚看到celery看起来不错,不知道limodou有木有试过.

--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+unsubscribe@googlegroups.com.

limodou

unread,
Jun 24, 2015, 7:19:56 AM6/24/15
to uliweb
在uliweb.contrib下已经有了

On Wed, Jun 24, 2015 at 2:50 PM, Chunlin Zhang <zhangc...@gmail.com> wrote:
现在这块咋样了呢?

在 2014年10月24日星期五 UTC+8上午7:47:10,limodou写道:
我没有。我正琢磨着自已写个简单的呢。

On Thu, Oct 23, 2014 at 10:57 PM, Chunlin Zhang <zhangc...@gmail.com> wrote:
上次在Q群里讨论过异步任务,我刚看到celery看起来不错,不知道limodou有木有试过.

--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+un...@googlegroups.com.



--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
UliWeb <<simple web framework>>: https://github.com/limodou/uliweb
My Blog: http://my.oschina.net/limodou

--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+un...@googlegroups.com.

To post to this group, send email to uli...@googlegroups.com.
Visit this group at http://groups.google.com/group/uliweb.

For more options, visit https://groups.google.com/d/optout.

Chunlin Zhang

unread,
Jun 24, 2015, 10:38:45 AM6/24/15
to uli...@googlegroups.com
大概看了一下,不是很懂,能不能简单说一下用法?

On Wed, Jun 24, 2015 at 7:19 PM, limodou <lim...@gmail.com> wrote:
在uliweb.contrib下已经有了

limodou

unread,
Jun 24, 2015, 7:56:18 PM6/24/15
to uliweb
先简单说一下使用,回头再补文档:

1. 启用celery,需要在settings.ini中的INSTALLED_APPS中添加 'uliweb.contrib.celery',同时要安装celery包
2. uliweb.contrib.celery本身提供了一些配置项

[CELERY]
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/1'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT=['json']
CELERY_ENABLE_UTC = True
CELERYBEAT_SCHEDULE = {}

[BINDS]
celery.after_init_apps = 'after_init_apps', 'uliweb.contrib.celery.after_init_apps'

[FUNCTIONS]
async_task = 'uliweb.contrib.celery:app.task'
async_run = 'uliweb.contrib.celery.tasks.common_celery_task'

其中BROKER_URL和CELERY_RESULT_BACKEND分别使用了redis,所以如果你继续使用redis,要自已安装redis,然后启动它。也可以按celery的文档换成其它的后端。CELERYBEAT_SCHEDULE目前主要是想用来实现一些调度的功能,但是这块还没有真正完成,可能要修改。

FUNCIONS中定义了两个函数,一个是用于将某个函数声明为celery的任务,而另一个则是不用声明直接以celery的方式来调用一个函数。

3. uliweb.contrib.celery 提供了命令行工具,可以通过 uliweb celery看到它的帮助。不过具体的celery的命令,还是要参考celery的文档,如启动celery服务为 : uliweb celery worker。好象-A app 不指定也可以。

4. celery的任务函数编写。如前面所说,uliweb的celery app提供了两种方法,一种是和celery一样,要声明。那么这样的任务可以写在每个app下的tasks.py中,例如:

from uliweb import functions

@functions.async_task
def add(x, y):
    return x+y

然后在调用的view中可以:

@expose('/celery')
def celery():
    from tasks import add

    result = add.delay(4,4)
    return result.get()

另一种写法是不用事先定义task,而是使用uliweb.celery定义好的一个方法来直接调用,如:

def p(name):
    return name

@expose('/celery_common')
def celery_common():
    result = functions.async_run.delay('test1.views:p', 'hello')
    return result.get()

上面代码p是一个普通函数,如果想要异步调用它,可以直接通过functions.async_run.delay()来调用,第一个参数是p这个函数的路径,采用 module:function_name的方式。后面是对应的参数。

不过这两种方法,async_task可以接受原celery可以接受的参数,这样就更灵活。而async_run则相当于预先写好了一个task,所以它的参数相对固定,不太灵活,可能对于简单使用足够了。你也可以仿照async_run的作法预定义一些固定的场景。



--
-- ----
Project : https://github.com/limodou/uliweb
doc : http://limodou.github.com/uliweb-doc
---
You received this message because you are subscribed to the Google Groups "Uliweb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to uliweb+un...@googlegroups.com.
To post to this group, send email to uli...@googlegroups.com.
Visit this group at http://groups.google.com/group/uliweb.

For more options, visit https://groups.google.com/d/optout.

Chunlin Zhang

unread,
Jun 24, 2015, 9:15:09 PM6/24/15
to uli...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages