This question has been flagged
4 Replies
10554 Views

Hello All,

How we can define thread for defined method which is called from button clicked?

I'm calling method from wizard which took long time to generate summarized data, because of long time execution the process is time out, so can i solve this problem?

I'm come up with the way thread, but i don't know how can i use with my method.

Any example?

Thanks

Prince.


Avatar
Discard
Best Answer

Hello Prince.
Below is the example.

import thread
from openerp.http import request
@api.multi
def button_method(self): #this is button method from wizard call
    #you need to set kwargs for db and uid
    kwargs = {'uid': request.uid, 'db': request.db}
    thread.start_new_thread(self.theard_method, (kwargs, arg1, args2, args_n)
    return {'type': 'ir.actions.act_window_close'}
def thread_method(self, kwargs, arg1, arg2, arg_n):    
new_cr = sql_db.db_connect(kwargs.get('db')).cursor()
with Environment.manage():
#you need to define environment as you can't use the self.env['']
env = Environment(new_cr, kwargs.get('uid'), {})
partner_obj = env['res.partner'] #example to initiate the obj

Avatar
Discard
Author

@kazim, thanks for reply but what is kwargs, args1 etc? it does not working from my side.

Kwargs is used to get the uid, db as you will need it in second method .

The thread will make the connection connection lost from the button method so it will need to declare the kwargs with db, uid

arg1, arg2 etc will be needed if you want to have the argument on the thread method. if you dont want any argument then you should not declare it.

Hope this helps

Best Answer

when you click on button then you have to call another method, and code define in current method cut that code and past it into method of thread.

import threading


def thread_method(self):

    #Code of method

    return True


****************suppose below method is which, when you click on button**********************

@api.one

def test_thread(self):

    # if you have created code here then move that code from here and past it in thread_method declare above

    thread_var = threading.Thread(target=self.thread_method)

    thread_var.start()

    return True


Avatar
Discard