跳至内容
菜单
此问题已终结
2 回复
6221 查看

I was wondering how you would go about creating a thread to do a background task? Say you have a function which takes roughly 5 seconds to complete, how could this be done in the background without slowing down the main process?


def  some_function(self):
    # do something
    self.function_which_takes_5_seconds()

def function_which_takes_5_seconds(self)
    # 5 seconds logic

形象
丢弃
最佳答案
from odoo import api, models, _
from concurrent.futures import ThreadPoolExecutor, as_completed

Se define un modelo con dos metodos:

class Report(models.AbstractModel):
​def long_time_method(self,parm1, parm2):
​# do something with long time
​# if you need made a search inside this method must do
​new_cr = self.pool.cursor()
​self = self.with_env(self.env(cr=new_cr))
​# here put your querry
​query = ['&','&',('type_template_id','in',mark_tpl_ids), ​('class_division_id','=',sheet.class_division_id.id), ​('academic_year_id','=',sheet.academic_year_id.id)]
​result = self.env['your.model'].search(querry).with_env(self.env(cr=new_cr))
​new_cr.commit()
​# IMPORTANT to close the cursor
​new_cr.close()

​def main_method (self):
​# simulate pool of parameters
​params ={
​parm1_a:parm2_a,
​parm1_b:parm2_b,
​parm1_c:parm2_c,
​}
​#Se crea el pool
​with ThreadPoolExecutor(max_workers=3) as executor:
​futures = {executor.submit(self.bedSheetTable,parm1,parm2):cualquier_dato for parm1,parm2 in params.items()}
​for future in as_completed(futures):
​result = future.result()
​# do someting with result


形象
丢弃
相关帖文 回复 查看 活动
1
8月 22
4406
3
9月 21
8510
0
8月 21
3317
4
7月 16
11684
0
4月 23
1894