回答我自己的问题:
threading
&
Odoo web service API
为了实现我的目标。
# -*- coding: utf-8 -*-
import xmlrpclib
import logging
from functools import partial
from odoo import _, api, fields, models, registry, SUPERUSER_ID
_logger = logging.getLogger(__name__)
class BaseReport(models.TransientModel):
_name = 'base.report'
_description = 'Base Object for All Reports'
@api.multi
def action_print(self):
qms_url = self.env['ir.config_parameter'].get_param('web.qms.url')
import threading
# call the maintainence_function here
t = threading.Thread(target=self.execute_queue, args=[qms_url])
# setDaemon=False to stop the thread after complete
t.setDaemon(False)
# starting the thread
t.start()
return True
@api.multi
def execute_queue(self, qms_url):
try:
_logger.info("Queue [{}] Execution Starting !".format(self.id))
self.ensure_one()
common = xmlrpclib.ServerProxy('{}/xmlrpc/2/common'.format(qms_url))
models = xmlrpclib.ServerProxy('{}/xmlrpc/2/object'.format(qms_url))
uid = common.authenticate(self.env.cr.dbname, '{your_username}', '{your_userpassword}', {})
models.execute_kw(self.env.cr.dbname, uid, '{your_userpassword}', 'queue.management.line', 'execute_thread', [self.id])
_logger.info("Queue [{}] Execution Done Successfully !".format(self.id))
except Exception as Ex:
_logger.exception("Queue [{}] Execution Failed !".format(self.id))
finally:
return True
@api.model
def execute_thread(self, queue_id):
def run_queue(queue_id, dbname):
db_registry = registry(dbname)
with api.Environment.manage(), db_registry.cursor() as cr:
env = api.Environment(cr, SUPERUSER_ID, {})
try:
{do_your_action_here}
except Exception as Ex:
_logger.exception("Queue [{}] Execution Failed !".format(queue_id))
self.env.cr.after("commit", partial(run_queue, queue_id, self.env.cr.dbname))
return True
希望对你也有帮助。