Search Here

Use Multi Threading to Seed Up Processing in Odoo

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.



Multi-threading or multiprocessing in odoo

What is 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.

Why we use multi-threading in odoo?

For example we want to insert huge amount of data in database table 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.

Use Multi Threading to Seed Up Processing in Odoo


1- Import python libraries for multi-threading


2- Create a function 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 that data in a variable.


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]]      
 

3- In next step we will use that variable as an argument to 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.

multi_threading_to_speed_up_process_in_odoo.py


Post a Comment

2 Comments

  1. i called function from _run_process function, every self crash after that .
    how can i manage it?

    ReplyDelete