mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-06 14:38:12 +08:00
Splits options from keywords through iteration
Popping options from async kwargs will make any reference to them void. Which can lead to unwanted situations. Unfortunately deepcopy clashes with the multiprocessing so we resort to simple re-iteration.
This commit is contained in:
+30
-22
@@ -1,5 +1,4 @@
|
|||||||
"""Provides task functionality."""
|
"""Provides task functionality."""
|
||||||
from copy import deepcopy
|
|
||||||
from multiprocessing import Queue, Value
|
from multiprocessing import Queue, Value
|
||||||
|
|
||||||
# django
|
# django
|
||||||
@@ -17,36 +16,45 @@ from django_q.brokers import get_broker
|
|||||||
|
|
||||||
def async(func, *args, **kwargs):
|
def async(func, *args, **kwargs):
|
||||||
"""Queue a task for the cluster."""
|
"""Queue a task for the cluster."""
|
||||||
# get options from q_options dict or direct from kwargs
|
keywords = {}
|
||||||
keywords = deepcopy(kwargs)
|
options = {}
|
||||||
options = keywords.pop('q_options', keywords)
|
opt_keys = ('hook', 'group', 'save', 'sync', 'cached', 'iter_count', 'iter_cached', 'chain', 'broker')
|
||||||
broker = options.pop('broker', get_broker())
|
# split keywords and options from kwargs and q_options
|
||||||
# pop optionals
|
if 'q_options' in kwargs:
|
||||||
opt_keys = {'hook': None,
|
for key, value in kwargs.items():
|
||||||
'group': None,
|
if key != 'q_options':
|
||||||
'save': None,
|
keywords[key] = value
|
||||||
'sync': None,
|
for key, value in kwargs['q_options'].items():
|
||||||
'cached': Conf.CACHED,
|
if key in opt_keys:
|
||||||
'iter_count': None,
|
options[key] = value
|
||||||
'iter_cached': None,
|
else:
|
||||||
'chain': None}
|
for key, value in kwargs.items():
|
||||||
for key in opt_keys:
|
if key in opt_keys:
|
||||||
opt_keys[key] = options.pop(key, opt_keys[key])
|
options[key] = value
|
||||||
# get an id
|
else:
|
||||||
|
keywords[key] = value
|
||||||
|
# get an id
|
||||||
tag = uuid()
|
tag = uuid()
|
||||||
# build the task package
|
# build the task package
|
||||||
task = {'id': tag[1], 'name': tag[0],
|
task = {'id': tag[1],
|
||||||
|
'name': tag[0],
|
||||||
'func': func,
|
'func': func,
|
||||||
'args': args,
|
'args': args,
|
||||||
'kwargs': keywords,
|
'kwargs': keywords,
|
||||||
'started': timezone.now()}
|
'started': timezone.now()}
|
||||||
|
# don't serialize the broker
|
||||||
|
broker = options.pop('broker', get_broker())
|
||||||
|
# overrides
|
||||||
|
if 'cached' not in options and Conf.CACHED:
|
||||||
|
options['cached'] = Conf.CACHED
|
||||||
|
if 'sync' not in options and Conf.SYNC:
|
||||||
|
options['sync'] = Conf.SYNC
|
||||||
# push optionals
|
# push optionals
|
||||||
for key in opt_keys:
|
for key in options:
|
||||||
if opt_keys[key] is not None:
|
task[key] = options[key]
|
||||||
task[key] = opt_keys[key]
|
|
||||||
# sign it
|
# sign it
|
||||||
pack = signing.SignedPackage.dumps(task)
|
pack = signing.SignedPackage.dumps(task)
|
||||||
if task.get('sync', False) or Conf.SYNC:
|
if task.get('sync', False):
|
||||||
return _sync(pack)
|
return _sync(pack)
|
||||||
# push it
|
# push it
|
||||||
broker.enqueue(pack)
|
broker.enqueue(pack)
|
||||||
|
|||||||
Reference in New Issue
Block a user