콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
4 답글
12195 화면

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.


아바타
취소
베스트 답변

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.

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

베스트 답변

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


아바타
취소
관련 게시물 답글 화면 활동
1
7월 16
5455
1
8월 22
4811
8
9월 15
5749
2
12월 23
7152
0
1월 19
4045