• 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'}
    

    其他办法:

    放弃使用env,直接使用sql原生语句查询,
    new_cr = self.pool.cursor()
    new_cr.execute('select * from res_partner')
    res = new_cr.fetchall()

  • 相关阅读:
    ES6-Generator
    ES6-Iterator & for...of循环
    ES6-Proxy and Reflect
    在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)
    函数的调用(取两个整型变量中的最大值)
    将一个字符串数组的元素的顺序进行翻转。。
    枚举类型练习
    利用Arraylist输入学生的成绩,求出平均分和总分。
    简单的推箱子游戏(利用数组)
    枚举类型的声明
  • 原文地址:https://www.cnblogs.com/qianxunman/p/12205170.html
Copyright © 2020-2023  润新知