从现有的装饰器创建一个新的装饰器
Create a new decorator from an existing one
我正在使用 dramatiq for my task queue which offers the decorator @dramatiq.actor
将函数装饰为任务。我尝试编写自己的装饰器来包装 @dramatiq.actor
装饰器,这样我就可以向 @dramatiq.actor
装饰器添加一个适用于所有任务的默认参数(我正在谈论的参数是 priority=100
).
出于某种原因,我收到以下错误:
TypeError: foobar() takes 1 positional argument but 3 were given
如果我将我的自定义 @task
装饰器切换为 @dramatiq.actor
它会起作用,所以我猜我的自定义装饰器不正确,但我无法发现我的错误。
decorators.py
def task(func=None, **kwargs):
def decorator(func):
@wraps(func)
@dramatiq.actor(priority=100, **kwargs)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
if func is None:
return decorator
return decorator(func)
tasks.py
@task
def foobar(entry_pk):
...
views.py
foobar.send_with_options(args=(entry.pk,))
用起来会方便很多functools.partial
:
from functools import partial
task = partial(dramatiq.actor, priority=100)
@task
def foobar(*args, **kwargs):
...
这允许您抢先向函数添加参数,而无需实际调用它。
另一种方法是子class Dramatiq
class 并覆盖actor
方法。该方法加上此处描述的其他一些技巧 - https://blog.narrativ.com/converting-celery-to-dramatiq-a-py3-war-story-23df217b426
我正在使用 dramatiq for my task queue which offers the decorator @dramatiq.actor
将函数装饰为任务。我尝试编写自己的装饰器来包装 @dramatiq.actor
装饰器,这样我就可以向 @dramatiq.actor
装饰器添加一个适用于所有任务的默认参数(我正在谈论的参数是 priority=100
).
出于某种原因,我收到以下错误:
TypeError: foobar() takes 1 positional argument but 3 were given
如果我将我的自定义 @task
装饰器切换为 @dramatiq.actor
它会起作用,所以我猜我的自定义装饰器不正确,但我无法发现我的错误。
decorators.py
def task(func=None, **kwargs):
def decorator(func):
@wraps(func)
@dramatiq.actor(priority=100, **kwargs)
def wrapper(*args, **kwargs):
return func(*args, **kwargs)
return wrapper
if func is None:
return decorator
return decorator(func)
tasks.py
@task
def foobar(entry_pk):
...
views.py
foobar.send_with_options(args=(entry.pk,))
用起来会方便很多functools.partial
:
from functools import partial
task = partial(dramatiq.actor, priority=100)
@task
def foobar(*args, **kwargs):
...
这允许您抢先向函数添加参数,而无需实际调用它。
另一种方法是子class Dramatiq
class 并覆盖actor
方法。该方法加上此处描述的其他一些技巧 - https://blog.narrativ.com/converting-celery-to-dramatiq-a-py3-war-story-23df217b426