What is multi-threading?

Why we use multi-threading in odoo?

import threading import uuid import openerp import openerp.release import openerp.sql_db import openerp.tools

def split_list(self,alist, wanted_parts=1): length = len(alist) return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts] for i in range(wanted_parts) ] A,B,C,D,E,F,G,H,I,J = split_list(result, wanted_parts=10)

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24], [25, 26, 27], [28, 29, 30]]

A_calculation = threading.Thread(target=self._run_process, args=(self.id, A)) B_calculation = threading.Thread(target=self._run_process, args=(self.id, B)) C_calculation = threading.Thread(target=self._run_process, args=(self.id, C)) D_calculation = threading.Thread(target=self._run_process, args=(self.id, D)) . . . . . J_calculation = threading.Thread(target=self._run_process, args=(self.id, J)) A_calculation.start() B_calculation.start() C_calculation.start() D_calculation.start() . . . . J_calculation.start()

def _run_process(self,active_id, list_of_ids): with api.Environment.manage(): with openerp.registry(self.env.cr.dbname).cursor() as new_cr: new_env = api.Environment(new_cr, self.env.uid, self.env.context) #use above new_env to perform multithreading # database insert/huge calculation code goes here

To use multi-threading in odoo we will use python threading module. Here remember that threads are different from processes. Threads run in the same memory (memory heap), on the other hand processes run on separate memory heap. Before jumping directly into the code we need to know about multi-threading.As we have discussed above that, threads run on same memory location so similarly multiple threads are also live/run on same memory spaces (heap memory). Each thread performs its own specific task.For example we want totable or we want to perform some huge calculations for all records on button click, in odoo that takes to much time, to improve this process we need multi-threading.Below are the steps to perform multi-threading in odoo on button click.1- Importlibraries for multi-threading2-that will convert our record ids into the multi dimensional arrays/list. For example if we have 30 records than this function will convert these records into the 10 arrays, and each array has 3 elements. In simple words we will split our data and assign that3- In next step we will use that variable as anto call python multi thread. Remember the number of thread will be same as the number of variable as we have created in step number 1.4- In each thread we have a function named "_run_process", this function will create a new env (environment) for us to perform processes in multi-threading. For the rest of the code we will use this newly created environment to perform process in batch or multi-threading mode.