Odoo Help

Welcome!

This community 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.

2

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

By
Prince Caspion
on 7/14/16, 10:04 PM 345 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.


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

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

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

@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


Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

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