Question

CELERYBEAT_SCHEDULE = {

    'task every 20 seconds': {
        'task': 'tasks.function',
        'schedule': timedelta(seconds=20),       
        'args': [argument] 
     },

}

My function takes one argument. But I want to call it for more than one argument on one worker. How can I call different arguments in "args:" or what I must do for define multiple arguments.

Was it helpful?

Solution

From your comment:

function(argument)-> what is workers execute. function(arg1), function(arg2) .... I want this:

you can accomplish this work in many ways, for this argument should be an iterator e.g. list, tuple:

1) Suppose if your function don't return any value just process argi ∈ arguments (for example prints) then you should use simple for loop, see following example (read comments):

>>> def f(arg):
...  print arg
... 
>>> arguments = ('aa', 'bb') # it is a tuple 
>>> for arg in arguments:
...  f(arg)
... 
aa  # f('aa')
bb  # f('bb')
>>> 

2) If suppose the function is something like it returns a value after processing arg passed to function, and you wants to collect all return values to store in some sequence e.g. list the you can call either list compression or map, check below example (read comments):

>>> def f(arg):
...   print "passed arguemnt is: ", arg
...   i = arg * 2
...   return i
... 
>>> arguments = (11, 22)
>>> L = map(f, arguments)  # (1) using map()
passed arguemnt is:  11  # f(11)
passed arguemnt is:  22  # f(22)
>>> L
[22, 44]
>>> L = [f(arg) for arg in arguments]  # (2) using list compression
passed arguemnt is:  11  # f(11)
passed arguemnt is:  22  # f(22)
>>> L
[22, 44]
>>> 

In last two tricks returned values are stored into a list L that I latter prints on command line interpreter.

The concept is still same is like other programming languages then you want to execute some statements repeatedly, you need loops:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top