Related to Async Task

I was working to understand asyn handler in frappe
In Async Tasks

  1. How we can add new Task in Async process, As we can not add any task from UI ?
  2. How i can attach handler to Async in from python code, right now i am using “override_whitelisted_methods” to perform this functionality ?

Can I get a bit more information that how I can add another frappe.async.handler
I added one more but it’s not getting included in cmd list of frappe.async command. Hopefully above 2 questions can help us to get answered these queries as well.

@shivnamha

Can you share some code? Also, describe what you are trying to achieve as it will give us the context to the question.

on the basis of submitting a sales Order, we are trying to raise 10 material requests automatically. So i am using frappeclient on same server.

#@celery_task()
#@frappe.async.handler
def copy_doc():
        # hack! pass event="bulk_long" to queue in longjob queue
        print frappe.flags
        #print doc 
        print frappe.local
        print frappe.__dict(args)
        form_dict = frappe._dict()
        print cmd 
        user = (frappe.session and frappe.session.user)
        if not pwd:
                return False
        client = FrappeClient("localhost", user)
        if not client:
                return
        copy_doc = frappe.new_doc(form_dict['doctype'])
       

I am calling this function from another document

def  mr_doc():
        user = (frappe.session and frappe.session.user) or doc_dict['modified_by'] or "Administrator"
        result = run_async_task.delay(site=frappe.local.site, user=user, cmd='sync_doc', form_dict=form_dict)

As above command is function definition not an another method, i am using functionality of async_ping().
but async_ping is defined as frappe.async.handler
when i am using same as command then it didnt worked.
Hopefully you can assist here.

you have to add your code to tasks.py in your app. It has to be tasks.py.

See this for example: https://github.com/frappe/erpnext/blob/develop/erpnext/tasks.py

Thanks allot rmehta
I have gone through this example, but how it will goes to async task queue automatically.
Can you give reference for it too.

Check celery help

Thanks Rmehta
I just found a method name run_async_task in async.py but its not getting used anywhere in frappe code. This functions creates Async Task Doctype document. Can you give brief information why its not getting used anywhere.

make sure this is not commented

@frappe.async.handler
def copy_doc():
    pass

also the run_async_task.delay should have cmd='copy_doc' and NOT cmd='sync_doc' since the function you have defined is copy_doc

Also, if prints dont work try adding this in your Procfile:
--loglevel DEBUG in the worker command

worker: sh -c 'cd sites && exec ../env/bin/python -m frappe.celery_app worker --loglevel DEBUG'

@pdvyas what do you think?

-Anand.

Thanks @anand
apologies for typo error as i have used cmd = 'copy_doc' only. and i have done change is configuration to enable debug level logs using -l debug.

I am calling this function mr_doc() from hooks.py in doc_events as given below

doc_events = { 
        "*": {
                "on_update":    "new_app.tasks.mr_doc",
              }
    }

I am getting this Error

 AttributeError: 'copy_doc' object has no attribute 'queue'
[2015-11-16 05:17:06,477: ERROR/Worker-6] frappe.tasks.run_async_task[2ca29d26-ec82-4c39-941f-983a5db9c57f]: Exception in running sync_doc: ["Traceback (innermost last):\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/tasks.py\", line 160, in run_async_task\n    execute_cmd(cmd, from_async=True)\n  File \"/home/frappe/frappe-bench/apps/frappe/frappe/handler.py\", line 92, in execute_cmd\n    method = method.queue\n  File \"/home/frappe/frappe-bench/env/local/lib/python2.7/site-packages/celery/local.py\", line 143, in __getattr__\n    return getattr(self._get_current_object(), name)\n AttributeError: 'copy_doc' object has no attribute 'queue'\n"]

I have gone through whole Code then i figure out that execute.cmd uses queue for method. How i can define this queue to execute this method
@pdvyas Hopefully you can assist here if i am missing any point to write this function.

@shivnamha I think this is going nowhere unless you share your full code. I have no idea what mr_doc does and how mr_doc and copy_doc are related.

Thanks @anand somehow i am able to manage to complete it using simple call of function from hooks.py instead of using Async process.