Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call a celery task within a Django view method?

I have two Pythonapplications 'frontend' and 'fv'. in my fv app is my tasks.py file and in my frontend app is my views.py file to render my views.

Now I have a view where I can choose some parameters get these with request.POST. and now I'd like to call a a task method FunctionRDynamic and pass the parameters from my view form.

Method in views.py:

if request.method == 'POST':
        form1 = dataproviderInstrumentForm(request.POST)
        form2 = dynamicTimeseriesForm(request.POST)

        if form1.is_valid() or form2.is_valid(): 
            filters = form2.cleaned_data['filter']
            estimator = form2.cleaned_data['estimator']
            windowSize = form2.cleaned_data['windowSize']

            FunctionRDynamic.delay(estimator, windowSize, timeseries)

FunctionRDynamic is my method in the tasks.py file in the oder application, but this method will not execute.

For my tasks I use celery. All is written in Python and I use Django as mvc framework.

Does anyone have suggestions?

like image 496
user2412771 Avatar asked Dec 16 '22 07:12

user2412771


1 Answers

From what I have understood here's the solution. While defining celery tasks do this:

@celery.task
def file_transfer(password, source12, destination):
    # Do stuffs with paramters

Now in your views.py do this:

def test(View):
    # Get the data from post
    if request.method == 'POST':
        name = request.POST['name']
        # And get all the variable you need for the tasks

        # Now call the task like this
        file_transfer.delay(name, 'test', 'test')
like image 80
Joyfulgrind Avatar answered Dec 27 '22 10:12

Joyfulgrind