Odoo Help


[odoo9] : How to use thread in method which is called from button?

Prince Caspion
on 7/14/16, 10:04 PM 1,512 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?




Kazim Mirza

--Kazim Mirza--
--Kazim Mirza--
Kazim Mirza
On 7/15/16, 1:32 AM

Hello Prince.
Below is the example.

import thread
from openerp.http import request
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

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

Prince Caspion
on 7/15/16, 3:02 AM

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

Kazim Mirza
on 7/15/16, 3:09 AM
Dilip Dabhi
On 7/15/16, 9:44 AM

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**********************


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)


    return True

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 7/14/16, 10:04 PM
Seen: 1512 times
Last updated: 7/20/16, 1:30 AM