odoo 多线程处理(AttributeError: environments)

Posted qianxunman

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了odoo 多线程处理(AttributeError: environments)相关的知识,希望对你有一定的参考价值。

可以查看odoo 源码的例子:

按常规的使用多线程调用方法,会出现环境错误.

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

#
# Order Point Method:
#    - Order if the virtual stock of today is below the min of the defined order point
#

from odoo import api, models, tools

import logging
import threading

_logger = logging.getLogger(__name__)


class StockSchedulerCompute(models.TransientModel):
    _name = 'stock.scheduler.compute'
    _description = 'Run Scheduler Manually'

    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('stock.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 {}

            for company in self.env.user.company_ids:
                self.env['procurement.group'].run_scheduler(
                    use_new_cursor=self._cr.dbname,
                    company_id=company.id)
            new_cr.close()
            return {}

    def procure_calculation(self):
        threaded_calculation = threading.Thread(target=self._procure_calculation_orderpoint, args=())
        threaded_calculation.start()
        return {'type': 'ir.actions.act_window_close'}

以上是关于odoo 多线程处理(AttributeError: environments)的主要内容,如果未能解决你的问题,请参考以下文章

Python 多处理库错误(AttributeError:__exit__)

Windows 上的 Python DEAP 和多处理:AttributeError

Python多处理错误:AttributeError:模块'__main__'没有属性'__spec__'

Python 多处理:AttributeError:“测试”对象没有属性“get_type”

Odoo 中的“工人”是啥?

Odoo v12 javascript双rpc调用问题