Skip to Content
Menu
This question has been flagged
2 Replies
6192 Views

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

Avatar
Discard
Best Answer
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


Avatar
Discard
Related Posts Replies Views Activity
1
Aug 22
4384
3
Sep 21
8492
0
Aug 21
3305
4
Jul 16
11661
0
Apr 23
1882