Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Odoo: Call self.env or another model in Thread

In odoo 10, I have a thread calling a method that uses self.env to call a model. But when the system does not let me occupy this and throws an exception like this:

Exception in thread Thread-44:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
  File "/home/kunder/proyectos/odoo/addons/kunder_odoo_comercionet/models/models.py", line 97, in update_quotations_thread
quotation_list = self.download_comercionet_docs();
  File "/home/kunder/proyectos/odoo/addons/kunder_odoo_comercionet/models/models.py", line 370, in download_comercionet_docs
new_last_date_db = (self.env["ftp_orders.settings"]).search([])[0].last_date
  File "/home/kunder/proyectos/odoo/odoo/models.py", line 1508, in search
res = self._search(args, offset=offset, limit=limit, order=order, count=count)
  File "/home/kunder/proyectos/odoo/odoo/models.py", line 4193, in _search
self.sudo(access_rights_uid or self._uid).check_access_rights('read')
  File "/home/kunder/proyectos/odoo/odoo/models.py", line 4842, in sudo
return self.with_env(self.env(user=user))
  File "/home/kunder/proyectos/odoo/odoo/api.py", line 781, in __call__
return Environment(cr, uid, context)
  File "/home/kunder/proyectos/odoo/odoo/api.py", line 726, in __new__
env, envs = None, cls.envs
  File "/home/kunder/proyectos/odoo/odoo/tools/func.py", line 111, in __get__
return self.fget.__get__(None, owner)()
  File "/home/kunder/proyectos/odoo/odoo/api.py", line 699, in envs
return cls._local.environments
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/local.py", line 72, in __getattr__
    raise AttributeError(name)
  AttributeError: environments

I handle the thread in a model and the way I handle the thread is:

def thread_method(self):
     #This method call other model like this and do some other stuff
     self.env['sale.order'].search([])
def main_method(self):
     threading.Thread(target = self.method_thread).start()

I'm debugging the code and if I seat on the main_method, it have the same object self.env that have the thread_method, so I do not understand why it does not works.

How can i request another model in a thread?

Thanks

like image 603
Gonzalo Garcia Avatar asked Feb 05 '23 04:02

Gonzalo Garcia


2 Answers

For threaded method you need to create your own environment

from openerp import models, api
from openerp.modules.registry import Registry 

def threaded_method(self):
    with api.Environment.manage():

check this for more https://github.com/odoo/odoo/blob/10.0/addons/stock/wizard/procurement_orderpoint_compute.py#L21

like image 181
Parth Gajjar Avatar answered Feb 07 '23 18:02

Parth Gajjar


you can find an example how to start a thread in procurement_orderpoint_compute.py file from Odoo addons.

def _procure_calculation_orderpoint(self):
    with api.Environment.manage():
        # As this function is in a new thread, I need to open a new cursor, because the old one may be closed
        new_cr = self.pool.cursor()
        self = self.with_env(self.env(cr=new_cr))
        scheduler_cron = self.sudo().env.ref('procurement.ir_cron_scheduler_action')
        # Avoid to run the scheduler multiple times in the same time
        try:
            with tools.mute_logger('odoo.sql_db'):
                self._cr.execute("SELECT id FROM ir_cron WHERE id = %s FOR UPDATE NOWAIT", (scheduler_cron.id,))
        except Exception:
            _logger.info('Attempt to run procurement scheduler aborted, as already running')
            self._cr.rollback()
            self._cr.close()
            return {}

        self.env['procurement.order']._procure_orderpoint_confirm(
            use_new_cursor=new_cr.dbname,
            company_id=self.env.user.company_id.id)
        new_cr.close()
        return {}

Or maybe better if you consider to use OCA queue

like image 32
Hoang Avatar answered Feb 07 '23 17:02

Hoang