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

import threading
class SampleThread (threading.Thread):
   def __init__(self, cr, uid, arg1, arg2, context=None): = cr
      self.uid = uid
      self.arg1 = arg1
      self.arg2 = arg2
      self.context = context

   def run(self): = pooler.get_db(
      my_instance = pooler.get_pool('sale.order')
      my_instance._do_action(, self.uid, self.arg1, self.arg2, self.context)

Step 2- Calling Threads

Threads can now be called from main function-

def _call_threads(self,cr,uid,ids,context={})
   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.