确认发票时的主题

时间:2018-11-12 15:34:34

标签: odoo odoo-9

创建发票时,如果发票行数超过17条,我想将其添加到线程中

        @api.multi
        def _run_threaded_invoice(self, ):
            with Environment.manage():
                new_cr = self.pool.cursor()
                self = self.with_env(self.env(cr=new_cr))
                self.action_invoice_open()
                new_cr.commit()
                new_cr.close()


    @api.multi
    def action_invoice_open(self):
        thread = False
        if self.type == 'in_invoice' and len(self.invoice_line_ids) > 17 and thread == False:
            thread = True
            threaded_calculation = threading.Thread(
                target=self._run_threaded_invoice)
            threaded_calculation.start()



           #and other logic that i have added to this method 

但我确实收到错误

TransactionRollbackError: could not serialize access due to concurrent update

PoolError: The Connection Pool Is Full

这对于递归错误来说我很熟悉...

我如何正确设置线程?

1 个答案:

答案 0 :(得分:1)

如果是递归的,请使用上下文来防止重复相同的操作

@api.multi
def _run_threaded_invoice(self, ):
    with Environment.manage():
        new_cr = self.pool.cursor()
        self = self.with_env(self.env(cr=new_cr))
        # add the key to the context
        self.with_context(is_in_thread=True).action_invoice_open()
        new_cr.commit()
        new_cr.close()




@api.multi
def action_invoice_open(self):
    is_in_thread = self.env.context('is_in_thread', False)
    if not is_in_thread and self.type == 'in_invoice' and len(self.invoice_line_ids) > 17:
        threaded_calculation = threading.Thread(
            target=self._run_threaded_invoice)
        threaded_calculation.start()
        return True # Let the thread do the job

    # this logic is execute by the new thread if > 17 or the main thread  <17   
    .......
    .......
    ......