Threading in OpenERP can be very useful especially on Gunicorn instance as it helps in accomplishing a task ‘n’ times faster where n is the number of concurrent threads running.
Step 1- Define custom Thread class
class SampleThread (threading.Thread):
def __init__(self, cr, uid, arg1, arg2, context=None):
self.cr = cr
self.uid = uid
self.arg1 = arg1
self.arg2 = arg2
self.context = context
self.cr = pooler.get_db(self.cr.dbname).cursor()
my_instance = pooler.get_pool(self.cr.dbname).get('sale.order')
my_instance._do_action(self.cr, self.uid, self.arg1, self.arg2, self.context)
Step 2- Calling Threads
Threads can now be called from main function-
threads = 
for i in range(0,no_of_process):
thread = SampleThread(cr,uid,arg1,arg2,context)
threads += [thread]
// Remaining Code
no_of_process is the number of concurrent threads we wish to run.
Step 3- Wait for the threads to terminate
We now need to ensure that all the threads have completed, so that the remaining code in the _call_threads function executes. For that, we do-
for thread in threads:
If we don’t do this, then the calling thread and called threads executes simultaneously.